Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@sumor/api-middleware
Advanced tools
API Middleware is a middleware for Node.JS. It can easily expose function to api, and validate parameters
A Sumor Cloud Tool.
More Documentation
API Middleware is a middleware for Node.JS. It can easily expose function to api, and validate parameters
npm i @sumor/api-middleware --save
Require Node.JS version 18.x or above
As this package is written in ES module,
please change the following code in your package.json
file:
{
"type": "module"
}
plus.js
in your project folder api
export default async (context, req, res) => {
const { data } = context
const { a, b } = data
return a + b
}
plus.json
in your project folder api
{
"name": "plus",
"parameters": {
"a": {
"name": "parameter a",
"type": "number",
"length": 3
},
"b": {
"name": "parameter b",
"type": "number"
}
}
}
index.js
fileimport express from 'express'
import apiMiddleware from '@sumor/api-middleware'
const app = express()
await apiMiddleware(app, process.cwd() + '/api')
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000')
})
node index.js
curl -X POST http://localhost:3000/plus -H "Content-Type: application/json" -d '{"a": 1, "b": 2}'
or use browser to open http://localhost:3000/plus?a=1&b=2
import express from 'express'
import apiMiddleware from '@sumor/api-middleware'
const app = express()
await apiMiddleware(app, process.cwd() + '/api', {
prefix: '/api',
prepare: async context => {
// do something before api
},
finalize: async (context, result) => {
// do something after api
},
exception: async (context, error) => {
// handle error
}
})
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000')
})
You can use yaml file to define config file, replace plus.json
with plus.yml
name: plus
parameters:
a:
name: parameter a
type: number
length: 3
b:
name: parameter b
type: number
For support js function in config file, you can use config.js
file, replace plus.json
with plus.config.js
export default {
name: 'plus',
parameters: {
a: {
name: 'parameter a',
type: 'number',
length: 3
},
b: {
name: 'parameter b',
type: 'number',
rule: [
{
code: 'TOO_BIG',
message: 'b should be less than 100',
function: function (value) {
return value < 100
}
}
]
}
}
}
You can reference below example to apply rules to parameters
{
"name": "plus",
"parameters": {
"a": {
"name": "parameter a",
"type": "number",
"length": 3,
"rule": [
{
"code": "GREATER_THAN_0",
"expression": "^[1-9][0-9]*$",
"message": "must be greater than 0"
}
],
"i18n": {
"zh": {
"GREATER_THAN_0": "必须大于0"
}
}
},
"b": {
"name": "parameter b",
"type": "number"
}
}
}
For more usage, please reference Validator
It includes all parameters passed in the request
file upload will be parsed as below object:
name
uploaded file namesize
uploaded file size(bytes)mime
uploaded file mime type(e.g. image/png)encoding
uploaded file encoding(e.g. 7bit)path
uploaded file pathIt includes all exposed apis
FAQs
API Middleware is a middleware for Node.JS. It can easily expose function to api, and validate parameters
The npm package @sumor/api-middleware receives a total of 55,924 weekly downloads. As such, @sumor/api-middleware popularity was classified as popular.
We found that @sumor/api-middleware demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.