美文网首页
004.koa2框架

004.koa2框架

作者: cmd_ts | 来源:发表于2019-01-23 10:10 被阅读4次
    aaa.png

    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;
    

    相关文章

      网友评论

          本文标题:004.koa2框架

          本文链接:https://www.haomeiwen.com/subject/mgfjjqtx.html