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
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
Share Improve this question asked Jul 13, 2017 at 15:43 thomasthomas 1,1742 gold badges14 silver badges31 bronze badges 2- Have you tried killing and restarting the app after you've made your changes? – atomrc Commented Jul 13, 2017 at 15:48
- 1 Come on, of course. Having said that, I wouldn't be surprised if it's a really silly mistake, something like you suggested... – thomas Commented Jul 13, 2017 at 15:53
3 Answers
Reset to default 4Now 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");
I've been getting the same error but i found out that maybe this is the easiest way of doing it.
'strict'
const koa = require('koa')
const app =new koa()
var route = require('koa-router');
const host = 'localhost' || '127.0.0.1';
const port = 3123 || process.env.PORT;
// initiate the route
var my_route = route();
// defines the route
my_route.get('/',function(body, next){
body.body = "I love routes"
});
app.use(my_route.routes())
app.listen(port, host,(err)=>{
if(err){
throw err;
}
console.log("The server has started")
})
I know this is old but for anyone who is new in koa, and beside using the new way in koa 3, as i had same error because lot of documents and examples just confuse me, it is said that routes should be last middleare to use but not before settings routes so you should write the line of using routes middlware before the code setter of routes like that:
const koa = require('koa'),
router = require('koa-router')();
var app = new koa();
app.use(router.routes())
.use(router.allowedMethods());
router.get('/', function *(next) {
this.body = "Hello"
});
app.listen(3000);
console.log("Listening on port 3000");
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744783972a4593509.html
评论列表(0条)