基于expressjs的老项目翻新方案

刚开始接触这方面的项目时,对ES规范理解不深,查了一些资料,发现如果不改expressjs的代码,大概率是没法用到最新的async/await了,后续也就没有继续往这个方面想。

这两天突然想起这个问题,祭出Google,用关键字一查,居然找到了答案。

A dead simple ES6 generators and ES7 async/await support hack for ExpressJS。

https://github.com/MadRabbit/express-yields

const express = require('express');
const yields = require('express-yields');
const User = require('./models/user');
const app = express();

app.get('/users', function* (req, res) {
  const users = yield User.findAll(); // <- some Promise
  res.send(users);
});

// or with node 7 async/await
app.get('/users', async (req, res) => {
  const users = await User.findAll(); // <- some Promise
  res.send(users);
});

有趣的是这个项目是两三年前就存在的,但却没有人及时的将类似的解决方案引进项目来。

猜你喜欢

转载自www.cnblogs.com/x3d/p/10534832.html