我非常喜欢将NodeJS与Express结合使用。我正在尝试使用Bootstrap组件创建一个简单的网站。我使用Express生成器生成了以下文件夹结构
Express(版本4.13.1) NodeJS(版本0.10.25)
一切正常,直到我在app.js文件中添加一些额外的路线。
APP.JS
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var routes = require('./routes/index');
var oudRijswijk = require('./routes/oudRijswijk');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(app.router);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//app.use('/', routes);
routes.initialize(app);
app.use('/OudRijswijk', oudRijswijk);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
在文件夹中使用命令“npm start”启动NodeJS服务器后,我收到以下错误:
Error: 'app.router' is deprecated!
Please see the 3.x to 4.x migration guide for details on how to update your app.
at EventEmitter.Object.defineProperty.get (/home/erik/git/expresswebsite`/node_modules/express/lib/application.js:123:13)`
at Object.<anonymous> (/home/erik/git/expresswebsite/app.js:22:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/erik/git/expresswebsite/bin/www:7:11)
at Module._compile (module.js:456:26)
npm ERR! weird error 8
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
我希望你能帮我解决这个问题。
非常感谢
埃里克
答案 0 :(得分:9)
根据迁移指南,已删除手动执行app.use(app.router)的需要。 现在你可以直接使用(例如):
app.get('/'...); app.post(...);
之后不添加'app.use(app.router)'行。
祝你好运, 爱德华