@sumor/api-middleware
Advanced tools
Comparing version 1.0.7 to 1.0.8
{ | ||
"name": "@sumor/api-middleware", | ||
"description": "API Middleware is a middleware for Node.JS. It can easily expose function to api, and validate parameters", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "repository": "sumor-cloud/api-middleware", |
@@ -77,4 +77,5 @@ # api-middleware | ||
const app = express() | ||
apiMiddleware(app, process.cwd() + '/api') | ||
await apiMiddleware(app, process.cwd() + '/api') | ||
app.listen(3000, () => { | ||
@@ -91,2 +92,35 @@ console.log('Server is running on http://localhost:3000') | ||
5. Test the api | ||
```bash | ||
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` | ||
### Options for apiMiddleware | ||
```javascript | ||
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') | ||
}) | ||
``` | ||
### More Config File Types | ||
@@ -93,0 +127,0 @@ |
20013
224