javascript 'app.router' 已弃用!在简单的网站上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32565167/
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
'app.router' is deprecated! on simple website
提问by Erik hoeven
I'am totaly new to NodeJS in combination with Express. I am trying to make a simple website with Bootstrap components. I have generated the folowing folder structure with the Express generator
我对 NodeJS 与 Express 结合完全陌生。我正在尝试使用 Bootstrap 组件制作一个简单的网站。我已经使用 Express 生成器生成了以下文件夹结构
- app.js
- bin (folder)
- node_modules(folder)
- package.json
- public (folder with: css, javascript, img)
- routes (index.js, oudRijswijk.js)
- views (jade files)
- 应用程序.js
- bin(文件夹)
- node_modules(文件夹)
- 包.json
- 公共(文件夹包含:css、javascript、img)
- 路线 (index.js, oudRijswijk.js)
- 意见(玉文件)
Express(version 4.13.1) NodeJS (version 0.10.25)
Express(版本 4.13.1)NodeJS(版本 0.10.25)
All works fine until i add some extra routes in the app.js file.
一切正常,直到我在 app.js 文件中添加一些额外的路由。
APP.JS
应用程序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;
After starting the NodeJS server with the command "npm start" in the folder i get the following error:
在文件夹中使用命令“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
I hope you can help me with this problem..
我希望你能帮我解决这个问题..
Many thanks
非常感谢
Erik
埃里克
回答by deChristo
According to the migration guide, the need to manually do app.use(app.router)
has been removed.
Now you can use directly (e.g.):
根据迁移指南,手动执行的需要app.use(app.router)
已被删除。现在您可以直接使用(例如):
app.get('/' ...);
app.post(...);
without adding the line 'app.use(app.router)'
after it.
没有'app.use(app.router)'
在它后面添加行。
Best regards, Eduardo
最好的问候,爱德华多