Search code examples
koa2

Koa start error


......\node_modules\koa\lib\response.js:47
const { res } = this;
      ^

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (......\node_modules\koa\lib\application.js:11:18)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

When I start my koa demo, console shows this.
node version: v4.4.7
"koa": "^2.2.0"
Here is my entry.js:

const Koa = require('koa');
const app = new Koa();

app.use(ctx => {
  ctx.body = 'Hello Koa';
});

app.listen(3000);

After I remove {} in response.js:47 and context.js:124

const res = this;

everything runs ok.I don't know why.


Solution

  • Because const res = this; mean just constant variable res equals this but const { res } = this; is object destructuring (http://exploringjs.com/es6/ch_destructuring.html) it's equal const res = this.res; So if this has not property res you'll receive error.