【发布时间】:2016-02-23 04:22:10
【问题描述】:
我使用 Koa 的最后一个 alpha 版本制作了这个简单的应用程序。为了使用 `async/wait Babel.Js 是必需的。
'use strict';
// babel registration (runtime transpilation for node)
require('./server.babel');
const Koa = require('koa');
const app = new Koa();
// define logger - this will be always executed
const logger = async (context, next) => {
const start = new Date;
await next();
const ms = new Date - start;
console.log(`${context.method} ${context.url} - ${ms}ms`);
}
const index = (context) => {
context.body = 'Hello World';
}
app.use(logger);
app.use(index);
app.listen(3000);
console.info(`The app is listening on port 3000`);
这是激活转译的钩子。
const fs = require('fs');
let config;
try {
config = JSON.parse(fs.readFileSync('./.babelrc'));
} catch (error) {
console.error('==> ERROR: Error parsing your .babelrc.');
console.error(error);
}
require('babel-core/register')(config);
这是配置文件:
{
"plugins": ["transform-async-to-generator"]
}
不幸的是,当我尝试运行该项目时,我收到以下错误:
const logger = async (context, next) => {
^
SyntaxError: Unexpected token (
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:404:25)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:138:18)
at node.js:974:3
我不知道为什么会出现此错误。我正在使用最新的 Node.Js 版本 5.1.0 和 Babel 6.2.1
【问题讨论】:
-
@Teemu 错字???在哪里?
-
@Teemu 字符串模板。新的 es6 功能
-
是你的“钩子”server.babel 吗?或者是别的什么
-
我的钩子在 server.babel 里面,你可以看到
-
我很确定 server.babel 需要你的应用代码,而不是相反。
标签: javascript node.js asynchronous ecmascript-6 babeljs