I'm trying to write my first ever koa.js app, and for some reason I just can't set a route with a function. I keep getting a "Not Found" error.
Here's my code -
const koa = require('koa'),
router = require('koa-router')();
var app = new koa();
router.get('/', function *(next) {
this.body = "Hello"
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
console.log("Listening on port 3000");
This code is based on the koa-router github example
Then when I go to localhost:3000 I get "Not Found"
What am I missing? Thanks
Now function generator is deprecate in koa2. Used code like
const koa = require('koa'),
router = require('koa-router')();
var app = new koa();
router.get('/', function(ctx, next) {
ctx.body = "Hello"
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);
console.log("Listening on port 3000");
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