Thursday, 11 May 2017

Named Parameters not Routing in Node using Koa 2

I reviewed and followed the guides of:

https://github.com/koajs/koa

https://github.com/alexmingoia/koa-router

https://github.com/saadq/koa-combine-routers/tree/next

I get 404 Not Found when attempting to use Named Parameters. What am I missing?

server.js

const Koa = require('koa');
const router = require('./routes');

const app = new Koa()

app.use(router);

app.listen(8080);

module.exports = app;

routes/index.js

const combineRouters = require('koa-combine-routers');
const paramRouter = require('./param');

const router = combineRouters([
  paramRouter
])

module.exports = router;

routes/param.js

const Router = require('koa-router');
const router = new Router()

router.get('/noParam/', async (ctx, next) => {
    ctx.body = ctx
});

router.get('/param/:jsonObj', async (ctx, next) => {
    ctx.body = ctx.params.jsonObj;
});

module.exports = router;

package.json

"dependencies": {
    "iorejson": "^0.1.1",
    "koa": "^2.2.0",
    "koa-combine-routers": "^1.0.0",
    "koa-router": "^7.1.1",
}

GET http://localhost:8080/noParam -> 200

GET http://localhost:8080/param?jsonObj=foo -> 404



via Ben Pingilley

No comments:

Post a Comment