我有这个普通的expressjs app.js
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
我要删除
app.get('/', routes.index);
app.get('/users', user.list);
并将它们放在一个单独的文件中。我正在尝试这个
module.exports = function(/* any dependency? */){
app.get('/', routes.index);
app.get('/users', user.list);
}
并在app.js
文件中我有require('./routed.js');
但这不起作用。我该如何解决这个问题?。
答案 0 :(得分:1)
我接受了krasu的回答并修改了一下
routed.js
var routes = require('./routes');
var user = require('./routes/user');
module.exports = function(app){
app.get('/', routes.index);
app.get('/users', user.list);
}
app.js
中的
require('./routed.js')(app);
这可行。
答案 1 :(得分:0)
试试这个:
app.use(express.static(path.join(__dirname, 'public')));
require('./routed.js')(app);
并在./routed.js
module.exports = function(app){
app.get('/', routes.index);
app.get('/users', user.list);
}