Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

koa.js basic app always returns Not Found

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" enter image description here

What am I missing? Thanks

like image 319
thomas Avatar asked Oct 17 '25 07:10

thomas


1 Answers

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");
like image 143
Rishabh Avatar answered Oct 19 '25 21:10

Rishabh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!