Open yreenchan opened 8 years ago
const Koa = require('koa'); const app = new Koa(); // logger app.use(async (ctx, next) => { const start = new Date; await next(); const ms = new Date - start; console.log(`${ctx.method} ${ctx.url} - ${ms}`); }); // response app.use(ctx => { ctx.body = 'Hello World'; }); app.listen(3000);
如上代码,用babel-node运行时,报下面错误
C:\Users\YreenChan\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel -core\lib\transformation\file\index.js:548 throw err; ^ SyntaxError: E:/xampp/htdocs/www/nodejs/koajs2/test.js: Unexpected token (6:26) [0m 4 | [90m// logger[39m 5 | > 6 | app[1m.[22muse[94m[1m([22m[39masync [94m[1m([22m[39mctx[1m,[22 m next[94m[1m)[22m[39m [1m=>[22m [32m{[39m | ^ 7 | [36mconst[39m start [1m=[22m [36mnew[39m Date[1m;[22m 8 | await next[94m[1m([22m[39m[94m[1m)[22m[39m[1m;[22m 9 | [36mconst[39m ms [1m=[22m [36mnew[39m Date [1m-[22m start[1m;[ 22m[0m at Parser.pp.raise (C:\Users\YreenChan\AppData\Roaming\npm\node_modules\babe l-cli\node_modules\babylon\index.js:1413:13) at Parser.pp.unexpected (C:\Users\YreenChan\AppData\Roaming\npm\node_modules \babel-cli\node_modules\babylon\index.js:2895:8) at Parser.pp.parseAsyncArrowFromCallExpression (C:\Users\YreenChan\AppData\R oaming\npm\node_modules\babel-cli\node_modules\babylon\index.js:593:47) at Parser.pp.parseSubscripts (C:\Users\YreenChan\AppData\Roaming\npm\node_mo dules\babel-cli\node_modules\babylon\index.js:544:21) at Parser.pp.parseExprSubscripts (C:\Users\YreenChan\AppData\Roaming\npm\nod e_modules\babel-cli\node_modules\babylon\index.js:511:15) at Parser.pp.parseMaybeUnary (C:\Users\YreenChan\AppData\Roaming\npm\node_mo dules\babel-cli\node_modules\babylon\index.js:481:19) at Parser.pp.parseExprOps (C:\Users\YreenChan\AppData\Roaming\npm\node_modul es\babel-cli\node_modules\babylon\index.js:412:19) at Parser.pp.parseMaybeConditional (C:\Users\YreenChan\AppData\Roaming\npm\n ode_modules\babel-cli\node_modules\babylon\index.js:394:19) at Parser.pp.parseMaybeAssign (C:\Users\YreenChan\AppData\Roaming\npm\node_m odules\babel-cli\node_modules\babylon\index.js:357:19) at Parser.pp.parseExprListItem (C:\Users\YreenChan\AppData\Roaming\npm\node_ modules\babel-cli\node_modules\babylon\index.js:1224:16)
如上代码,用babel-node运行时,报下面错误