app.js:
const Koa = require('koa');
const app = new Koa();
const bodyParser = require('koa-bodyparser');
const routers = require('./router/router');
app.use(bodyParser());
app.use(routers.routes(), routers.allowedMethods());
app.listen(3000,()=>{
console.log('[demo] server is starting at port 3000');
});
router.js:
const router = require('koa-router')();
const login = require('./routers/login');
const user = require('./routers/user');
router.use('/login', login.routes(), login.allowedMethods());
router.use('/user', user.routes(), user.allowedMethods());
module.exports = router;
login.js:
var router = require('koa-router')();
router.get('/', function (ctx) {
ctx.body = 'this a index response!';
});
router.get('/cmd', function (ctx) {
ctx.body = 'this a index/hi response!';
});
router.post('/denglu', function (ctx, next) {
ctx.body = ctx.request.body
console.log('123')
})
module.exports = router;
网友评论