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!
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With