命名参数不使用Koa 2在节点中路由

时间:2017-05-11 16:35:12

标签: node.js redis koa2 koa-router

我查看并遵循以下指南:

https://github.com/koajs/koa

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

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

尝试使用命名参数时,我发现404 Not Found。我错过了什么?

server.js

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

const app = new Koa()

app.use(router);

app.listen(8080);

module.exports = app;

路由/ index.js

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

const router = combineRouters([
  paramRouter
])

module.exports = router;

路由/ 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

1 个答案:

答案 0 :(得分:0)

我用来调用的方法是错误的。

GET http://localhost:8080/param/foo - > 200