Skip to content

Latest commit

 

History

History
113 lines (74 loc) · 1.65 KB

README.md

File metadata and controls

113 lines (74 loc) · 1.65 KB

Express Logo

express-es7

Note: Safely use async middleware in express.


Table of contents


Installation

$ npm install express-es7 --save

or

$ cnpm install express-es7 --save

Migrating

How to migrate express to express-es7

const express = require('express');
const app = express();

// ...

// use "express-es7" instead of "express", that is all

const express = require('express-es7');
const app = express();

Extra API

// 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) => {
  // ...
});

Usage

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) => {
  // ...
});

Examples