Express 中使用 Generator 方法 express-coroutine

码农软件 · 软件分类 · 服务器端JavaScript · 2019-04-13 12:42:58

软件介绍

在 Express 中像 Koa 那样使用 Generator 方法。

安装:

npm install express-coroutine --save

初始化:

// Init express

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

// Init router

const express = require('express-coroutine')(require('express'));
const app = express();
const router = new expressGenerators.Router();
app.use(router);

router.get('/', function* (req, res) {
  res.send('it works!');
});

使用:

const express = require('express-coroutine')(require('express'));

const app = express();

app.get('/user/:id', function* (req, res) {
  const user = yield User.findById(req.params.id);
  res.send(user);
})

app.get('/error', function* (req, res) {
  throw new Error('Bang!');
});

app.listen(8000);

更多用法详见测试文件:https://github.com/yourtion/express-coroutine/blob/master/test.js

本文地址:https://www.codercto.com/soft/d/3487.html

High Performance Python

High Performance Python

Andrew Lewis / O'Reilly Media, Inc. / 2010-09-15 / USD 34.99

Chapter 1. Introduction Section 1.1. The High Performance Buzz-word Chapter 2. The Theory of Computation Section 2.1. Introduction Section 2.2. Problems Section 2.3. Models of Computati......一起来看看 《High Performance Python》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具