问题描述
node版本是v6.2.2
新建一个app.js内容如下
async function responseTime(ctx, next) { const start = new Date(); await next(); const ms = new Date() - start; ctx.set(’X-Response-Time’, `${ms}ms`);}
然后node app.js报如下错误:
mbp:hybrid-statistic wfp$ node app.js/Users/wfp/Work/hybrid-statistic/app.js:28async function responseTime(ctx, next) { ^^^^^^^^SyntaxError: Unexpected token function at Object.exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:513:28) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:458:32) at tryModuleLoad (module.js:417:12) at Function.Module._load (module.js:409:3) at Module.runMain (module.js:575:10) at run (node.js:348:7) at startup (node.js:140:9) at node.js:463:3
怎么破?
问题解答
回答1:Node会在Chrome 55 Stable发布后正式支持async/await,不过最新的V7版本在打开--harmony的模式的时候可以支持.
回答2:用bable编译下吧
回答3:加这个试试 node --harmony app.js
回答4:nodeJS v7才支持