Can I run Koajs without the --harmony tag?

Since iojs merges with Node. I assumed that I could run koajs without the --harmony tag (because it will have es6 generators supported).

So in my server.js file I have:

 var koa = require('koa'); var app = koa(); app.use(function *(){ this.body = 'Hello World'; }); app.listen(3000); 

There is "koa": "^1.1.2" in my package.json file "koa": "^1.1.2" .

I run node server.js and get:

 app.use(function *(){ ^ SyntaxError: Unexpected token * 

Any ideas why he is complaining? Should I use the --harmony tag?

Thanks!

+5
source share
1 answer

I am surprised that I did not find any more questions about this on the Internet. In any case, I should work without the --harmony flag.

They are currently working on V2.* , Which has ES6 support. You can find it on your git repository in the V2 branch section https://github.com/koajs/koa .

So you need npm install koa@next -save to grab the last one that is currently "koa": "^2.0.0-alpha.3" .

To make sure that it works, you can quickly throw it into the index.js file, then run node index.js :

 const Koa = require('koa'); const app = new Koa(); // logger app.use((ctx, next) => { const start = new Date; return next().then(() => { const ms = new Date - start; console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); }); }); // response app.use(ctx => { ctx.body = 'Hello World'; }); app.listen(3000); 

V2, when stable will be merged into a master branch, and only npm install koa will work. But for what I wanted, npm install koa@next -save worked fine :)

+1
source

Source: https://habr.com/ru/post/1238140/


All Articles