我一直在编写我的第一个node.js / express测试网络应用程序,并在开发过程中面临路由问题(我想是这样,但我不确定)。以下是有关我的问题的一些调试信息。我正在使用Express 4并试图像这样连接路由器:
var router = express.Router();
...
app.use(router);
错误日志:
TypeError: undefined is not a function
at /Users/user1/Documents/work/habitsapp-node-app/app.js:77:11
at Layer.handle_error (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/index.js:310:13)
at /Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/index.js:330:12)
at next (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/index.js:271:10)
at next (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/route.js:121:14)
at Layer.handle [as handle_request] (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/layer.js:97:5)
at next (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/layer.js:95:5)
at /Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/index.js:330:12)
at next (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/lib/router/index.js:271:10)
at SendStream.error (/Users/user1/Documents/work/habitsapp-node-app/node_modules/express/node_modules/serve-static/index.js:120:7)
at SendStream.emit (events.js:107:17)
app.js文件
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var errorhandler = require('errorhandler');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var config = require('config');
var mongoose = require('lib/mongoose');
var session = require('express-session');
var log = require('lib/log')(module);
var HttpError = require('error').HttpError;
var multer = require('multer');
var router = express.Router();
var app = express();
// view engine setup
app.engine('ejs', require('ejs-locals'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static('public'));
var MongoStore = require('connect-mongo')(session);
app.use(session({
secret: config.get('session:secret'),
key: config.get('session:key'),
cookie: config.get('session:cookie'),
store: new MongoStore({mongoose_connection: mongoose.connection})
}));
app.use(require('middleware/sendHttpError'));
app.use(require('middleware/loadUser'));
app.use(router);
app.use(express.static(path.join(__dirname, 'public')));
require('routes')(app);
app.use(function(err, req, res, next) {
if (typeof err == 'number') {
err = new HttpError(err);
}
if (err instanceof HttpError) {
res.sendHttpError(err);
} else {
if (app.get('env') == 'development') {
app.errorHandler()(err, req, res, next);
} else {
log.error(err);
err = new HttpError(500);
res.sendHttpError(err);
}
}
});
var server = app.listen(config.get('port'), function () {
var host = server.address().address;
var port = server.address().port;
log.info('HabitTracker app listening on port ' + config.get('port'));
});
loadUser.js
var User = require('models/user').User;
module.exports = function(req, res, next) {
req.user = res.locals.user = null;
if (!req.session.user) return next();
User.findById(req.session.user, function(err, user) {
if (err) return next(err);
req.user = res.locals.user = user;
next();
});
};
答案 0 :(得分:1)
这行代码应为
if (app.get('env') == 'development') {
app.use(errorhandler());
} else {
log.error(err);
err = new HttpError(500);
res.sendHttpError(err);
}
答案 1 :(得分:0)
我不确定你在这做什么,但app.errorHandler
未定义。关注()
的{{1}}会导致出现错误消息app.errorHander
。
此外,您需要使用此代码TypeError: undefined is not a function
errorhandler
,但您将其引用为var errorhandler = require('errorhandler');
。你的外壳不匹配。
这与快递路由器或ejs无关。