Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run Koajs without the --harmony tag

Since iojs merged into Node. I assumed I can run koajs without the --harmony tag (because it'll have generators from es6 supported).

So within my server.js file I have:

var koa = require('koa');
var app = koa();

app.use(function *(){
  this.body = 'Hello World';
});

app.listen(3000);

My package.json file has "koa": "^1.1.2".

I run node server.js and get:

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

Any ideas why it's complaining? Do I still need to use the --harmony tag?

Thanks!

like image 626
pourmesomecode Avatar asked Nov 08 '22 23:11

pourmesomecode


1 Answers

I'm surprised I didn't come across more questions about this on the web. Anyway, i've got it to work without the --harmony flag.

At the moment they're working on V2.* which has ES6 support. You can find it on their git repo under V2 branch https://github.com/koajs/koa.

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

To make sure it's working you can quickly throw this in an 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 once stable will be merged into the master branch and just npm install koa will work. But for what I wanted, npm install koa@next -save worked fine :)

like image 105
pourmesomecode Avatar answered Nov 15 '22 06:11

pourmesomecode