Note: Safely use async middleware in express.
$ npm install express-es7 --save
or
$ cnpm install express-es7 --save
const express = require('express');
const app = express();
// ...
// use "express-es7" instead of "express", that is all
const express = require('express-es7');
const app = express();
// catch unhandledRejection error
// when using `express` not `express-es7`
const express = require('express');
const { capture, batch } = require('express-es7');
const app = express();
// avoid unhandledRejection error
app.use(capture(async (req, res, next) => {
}));
// batch processing
app.use(batch(middleware1, middleware2, middleware3, ...));
// catch error
app.use((err, req, res, next) => {
// ...
});
use ES2017 async functions as middleware with express
const express = require('express-es7');
const app = express();
// use async functions
app.use(async(req, res, next) => {
// todo your code
next();
});
// app.use(middleware[, ...middleware]);
// use router
const apiv2 = express.Router();
apiv2.get('/', async(req, res) => {
// todo your code
});
app.use('/api/v2', apiv2);
// catch error
app.use((err, req, res, next) => {
// ...
});