express TypeError:未定义不是一个函数
2015-07-27
1901
我对 node 和 express 不熟悉,当我运行我的应用程序时出现此错误。有什么解决方案吗?我认为错误出在这里“app.use(express.static(user));”,但我不确定。
var express = require('express');
var http = require('http');
var path = require('path');
var app = express();
// modulos
var home = require('./controllers/home');
var model = require('./models');
var user = require('./controllers/user');
// all environments
app.set('port', process.env.PORT || 3005);
app.set('views', __dirname + '/layouts');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(user));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// rutas
app.use(home);
错误
Express 500 TypeError:undefined 不是函数
at Object.posix.isAbsolute (path.js:479:15)
at posix.normalize (path.js:461:26)
at SendStream.root.SendStream.from (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/send/lib/send.js:116:16)
at Object.staticMiddleware [as handle] (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/connect/lib/middleware/static.js:79:8)
at next (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at resume (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/connect/lib/middleware/static.js:60:7)
at SendStream.error (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/connect/lib/middleware/static.js:73:37)
at SendStream.emit (events.js:107:17)
at SendStream.error (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/send/lib/send.js:147:51)
at SendStream.onStatError (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/send/lib/send.js:248:48)
at /Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/send/lib/send.js:320:26
at FSReqWrap.oncomplete (fs.js:95:15)
controllers/user/index.js
// JavaScript Document
var express = require('express');
var app = module.exports = express();
app.set('views', __dirname + '/views');
app.get('/user/new', function(request, response) {
response.render('new');
});
2个回答
不需要该行(
app.use(express.static(user));
)删除它。
您正在
controller/user/index.js
中定义新应用程序。
您设置的那些选项将不会设置到主应用程序。更改下面的代码。
main server.js
var user = require('./controllers/user')(app);
controllers/user/index.js
// JavaScript Document
module.exports = function (app){
app.set('views', __dirname + '/views');
app.get('/user/new', function(request, response) {
response.render('new');
});
}
Vishnu
2015-07-27
app.use(express.static(path.join(__dirname, 'controllers/user.js'));
Vishnu
2015-07-27