我无法使快速路由器与Babel v6使用async/await
函数。
它抛出了这样的错误:
MacBook-Pro-Dmitri:api dmitri$ npm start
> dhhb-api@0.1.0 start /Users/dmitri/github/dhhb/api
> node bin/runServer
/Users/dmitri/github/dhhb/api/node_modules/express/lib/router/route.js:196
throw new Error(msg);
^
Error: Route.post() requires callback functions but got a [object Undefined]
at Route.(anonymous function) [as post] (/Users/dmitri/github/dhhb/api/node_modules/express/lib/router/route.js:196:15)
at Function.proto.(anonymous function) (/Users/dmitri/github/dhhb/api/node_modules/express/lib/router/index.js:510:19)
at exports.default (index.js:41:13)
at exports.default (index.js:13:28)
at Object.<anonymous> (server.js:27:16)
at Module._compile (module.js:460:26)
at loader (/Users/dmitri/github/dhhb/api/node_modules/babel-core/node_modules/babel-register/lib/node.js:130:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/dmitri/github/dhhb/api/node_modules/babel-core/node_modules/babel-register/lib/node.js:140:7)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/dmitri/github/dhhb/api/bin/runServer.js:4:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
我的.babelrc
:
{
"presets": [
"es2015",
"stage-0"
],
"plugins": [
"syntax-async-functions",
"transform-regenerator",
"transform-runtime"
]
}
初始文件:
require('babel-core/register');
require('babel-polyfill');
require('../src/server.js');
服务器js端点:
// uncomment to throw
// app.get('/test1', async function () { // will throw });
app.get('/test2', function () { // no error thrown });
更新
我已经设置了一个演示回购以复制错误行为: