节点快递。路由db.get和find方法

时间:2015-06-24 20:32:19

标签: express

这是我的routes / users.js文件:

var express = require('express');
var router = express.Router();

/*
 * GET userlist.
 */
router.get('/userlist', function(req, res) {
    var db = req.db;
    var collection = db.get('userlist');
    collection.find({},{},function(e,docs){
        res.json(docs);
    });
});

module.exports = router;

在这段代码中,req对象是什么?当我们调用req.db时...在app.js中通过此权限提供给我们:

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest2');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.use(function(req,res,next){
    req.db = db;
    next();
});

这里的app.use到底是做什么用的?

0 个答案:

没有答案