@ awaitjs/ 表达
使用 Aync/awa 写入快取中件和路由处理器
使用量
const { addAsync } = require('@awaitjs/express');
// Or, if you want to use `wrap()`
const { wrap } = require('@awaitjs/express');
API 密码
添加Async( )
这个 addAsync() 函数是添加 async/await 的首选方式
支持您的 Express 应用程序。 此函数添加了多个帮助函数
你的快递应用程序.
它又说 useAsync(), (中文(简体) ), getAsync()等到您的快捷应用程序
这个 addAsync() 函数添加 useAsync(), (中文(简体) ), deleteAsync(), (中文(简体) ), getAsync(), (中文(简体) ),
headAsync(), (中文(简体) ), paramAsync(), (中文(简体) ), patchAsync(), (中文(简体) ), postAsync(),以及 putAsync()。 。 。 .
const express = require('express');
const app = addAsync(express());
// `useAsync()` is like `app.use()`, but supports async functions
app.useAsync(async function(req, res, next) {
await new Promise(resolve => setTimeout(resolve, 50));
});
// `getAsync()` is like `app.get()`, but supports async functions
app.getAsync('*', async function(req, res, next) {
throw new Error('Oops!');
});
// Because of `getAsync()`, this error handling middleware will run.
// `addAsync()` also enables async error handling middleware.
app.use(function(error, req, res, next) {
res.send(error.message);
});
const server = await app.listen(3000);
路由器( )
此模块导出 a Router() 函数为下移
替换为 express.Router(),但返回时除外
路由器已经 getAsync(), (中文(简体) ), useAsync()等 (中文(简体) ).
出口a Router 函数,该函数返回新的对合成友好路由器
const express = require('express');
const app = express(); // This app isn't async friendly.
const router = Router(); // But this router is.
router.getAsync('/foo', async function(req, res, next) {
throw new Error('Oops!');
});
app.use(router);
app.use(function(err, req, res, next) {
res.send(err.message);
});
const server = await app.listen(3000);
装饰App
无罪: 无罪: 结束
它是添加Async( ) 的同名词
assert.equal(decorateApp, addAsync)
装饰
它是添加Async( ) 的同名词
assert.equal(decorateRouter, addAsync)
包装( c)
如果你需要更多精细的控制 比什么 addAsync() 给
你,你可以使用这个 wrap() 函数。 此函数环绕一个同位素
为更好的错误处理而提供中间软件或路由处理器.
它与 Express- 兼容的处理出错 连接到一个同源函数
const express = require('express');
const app = express();
// `wrap()` takes an async middleware or route handler and adds a
// `.catch()` to handle any errors. It also prevents double-calling
// `next()`.
app.get('*', wrap(async function(req, res, next) {
throw new Error('Oops!');
}));
// `wrap()` also supports async error handling middleware.
app.use(wrap(async function(error, req, res, next) {
throw new Error('foo');
}));
app.use(function(error, req, res, next) {
res.send(error.message); // Will send back 'foo'
});
const server = await app.listen(3000);