app.js

var express = require('express');
var app = express();
app.use('/',require('./control'));
app.use('/',require('./data'));
app.use('/',require('./logic'));
app.listen(8000);

control.js

/**
 权限层
*/
var router = require('express').Router();

router.get('/',function(req,res,next){
   var group = req.query.group;
   if(group){
      next();
   }else{
      next('无权访问!');
   }
});
module.exports = router;

//如果这里的next(err)里面带有参数,则不会执行后面的路由

db.js

module.exports = {
   getUser(){

    //数据库数据

   },
   getNews(){
      //数据库数据
   }

data.js


/**
 数据层
 一般数据层不会直接和数据库挂钩,通常会由和数据库耦合的模块进行处理,然后传递数据
*/
var router = require('express').Router();
var dbs = require('./db');
router.get('/',function(req,res,next){
 console.log('next的值:',next);
 res.locals.name = '老王';
 next();
});
router.get('/info',function(req,res,next){
   dbs.getUser(function(user){
      dbs.getNews(function(news){
         res.locals.user = user;
         res.locals.news = news;
      });
   });
});
module.exports = router;

login.js

/**
 业务层
*/
var router = require('express').Router();

  router.get('/',function(req,res,next){
     var name = res.locals.name;
     var a = parseInt(req.query.a);
     var b = parseInt(req.query.b);
     res.send(name + (a+b));
  });
module.exports = router;

04-09 07:24