我有一个使用 Express 和 Mongoose 的节点应用程序。我在演示应用程序中看到了这一行,但我不明白它是如何工作的。
应用程序.js
require('./db.js');
require('./routes.js')(app);
数据库.js
exports = mongoose = require('mongoose');
mongoose.connect('localhost:27017/test');
exports = Schema = mongoose.Schema;
require('./models.js')
模型.js
var ArticleSchema = new Schema({
title : {type : String, default : '', trim : true}
, body : {type : String, default : '', trim : true}
, user : {type : Schema.ObjectId, ref : 'User'}
, created_at : {type : Date, default : Date.now}
})
mongoose.model('Article', ArticleSchema);
路由.js
var Article = mongoose.model('Article');
module.exports = function(app){
app.get('/new', function(req, res){
var article = new Article({});
res.render('new', article);
});
};
变量 mongoose 和 schema 在 models.js 和 routes.js 等其他模块中如何可用?
exports = mongoose = require('mongoose');
如果我将演示中看到的行更改为我更熟悉的任何一个,则此代码仍然有效。
module.exports = mongoose = require('mongoose');
exports.anything = mongoose = require('mongoose');
三个赋值中间的变量名是其他文件中可用的。
有人可以解释这里发生了什么以及它是如何工作的吗?
谢谢!