javascript 你如何使用 Express 模块化 Node.JS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5055853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you modularize Node.JS w/Express
提问by Kiran Ryali
I'm trying to modularize my node.js application (using express framework). The trouble I am having is when setting up my routes.
我正在尝试模块化我的 node.js 应用程序(使用 express 框架)。我遇到的麻烦是在设置路线时。
I am no longer able to extract the data I send to the post. (req.body is undefined). This works okay if it is all in the same file. What am I doing wrong here, and what is the best way to modularize code in node.js?
我无法再提取发送到帖子的数据。(req.body 未定义)。如果它们都在同一个文件中,这可以正常工作。我在这里做错了什么,在 node.js 中模块化代码的最佳方法是什么?
My app.js
我的 app.js
require('./routes.js').setRoutes(app);
My route.js
我的路线.js
exports.setRoutes = function(app){
app.post('/ask', function(req, res, next){
time = new Date();
var newQuestion = {title: req.body.title, time: time.getTime(), vote:1};
app.questions.push(newQuestion);
res.render('index', {
locals: {
title: 'Questions',
questions: app.questions
}
});
});
回答by ctide
A better approach:
一个更好的方法:
Create a routes.js file that contains:
创建一个 routes.js 文件,其中包含:
var index = require('./controllers/index');
module.exports = function(app) {
app.all('/', index.index);
}
Then, from within your server.js (or however you've started your server), you'd require it as such:
然后,从你的 server.js 中(或者你已经启动了你的服务器),你需要它:
require('./routes')(app);
This way you aren't creating global variables that bring with them a whole slew of issues (testability, collision, etc.)
这样你就不会创建全局变量,这些变量会带来一系列问题(可测试性、冲突等)。
回答by OneOfOne
I realize someone already answered but here's what I do anyway.
我意识到有人已经回答了,但这就是我所做的。
app.js :
应用程序.js:
fs.readdir('./routes', function(err, files){
files.forEach(function(fn) {
if(!/\.js$/.test(fn)) return;
require('./routes/' + fn)(app);
});
});
./routes/index.js :
./routes/index.js :
module.exports = function(app) {
var data_dir = app.get('data-dir');
app.get('/', function(req, res){
res.render('index', {title: 'yay title'}
});
}
Maybe someone will find this approach helpful.
也许有人会发现这种方法很有帮助。
回答by Kiran Ryali
My issue was that I was declaring app in the wrong way. Instead of var app = module.exports = express.createServer();, it should have just been app = express.createServer();
我的问题是我以错误的方式声明了应用程序。相反var app = module.exports = express.createServer();,它应该只是app = express.createServer();
And then all I needed to do in my app.js was require('./routes.js');. Now the routes file has access to the app variable and now I can just declare routes normally in the routes file.
然后我需要在 app.js 中做的就是require('./routes.js');. 现在路由文件可以访问 app 变量,现在我可以在路由文件中正常声明路由。
(routes.js)
(路线.js)
app.get('/test', function(req, res){
console.log("Testing getter");
res.writeHead('200');
res.end("Hello World");
});
回答by tjholowaychuk
global "app" for me (usually). If you know the app wont be require()d, and if you're not clumsy it's a lot easier to work with
对我来说全局“应用程序”(通常)。如果您知道该应用程序不会被 require()d,并且如果您不笨拙,那么使用它会容易得多
回答by ngakak
example in app.js
app.js 中的示例
var users = require('./lib/users'); //this is your own module
app.use(users);
then, you in your lib/users folder, create files ( index.js,user.ejs,etc). use index.js for default module load //index.js var express = require('express'); var app = module.exports = express();
然后,您在 lib/users 文件夹中创建文件(index.js、user.ejs 等)。使用 index.js 进行默认模块加载 //index.js var express = require('express'); var app = module.exports = express();
app.set('views',__dirname);
app.set('view engine','ejs');
app.get('/users',function(req,res){
//do stuffs here
});
I made an example of Modular node.jes + Boostrap here : Nodemonkeyor a tutor here here
我在这里提出模块化node.jes +自举的一个例子:Nodemonkey或导师在这里在这里

