javascript 使用 express node.js 的路由但 express.Router 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23544031/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:04:08  来源:igfitidea点击:

Using Route of express node.js but express.Router getting as undefined

javascriptnode.jsexpress

提问by Nish

My code of router from default routes/index

我的默认路由/索引中的路由器代码

/* GET home page. */
exports.index = function(req, res){
  res.render('user', { title: 'Abcd' });
};

var express = require('express');

var router = express.Router();

/* GET home page. */
router.get('/', function(req, res) {
    res.render('index', { title: 'Express' });
});

router.get('/helloworld', function(req, res) {
    res.render('helloworld', { title: 'Hello, World!' })
});

module.exports = router;

getting error as can not call method get of undefined.I am new in node js please anyone help me.

出现错误,因为无法调用 undefined 的方法 get。我是 node js 的新手,请任何人帮助我。

采纳答案by matthias

Router is a middlewareof express which is registered implicitly with the express object the first time post()or get()is used. You can but don't have to add this explicitly calling use(), which allows you to register various middleware with express and so allows configuring processing and behavior in consideration of precedence.

路由器是一种中间件,其与明示对象隐式注册在第一时间明示post()get()使用。您可以但不必添加此显式调用use(),它允许您使用 express 注册各种中间件,因此允许在考虑优先级的情况下配置处理和行为。

Correct initialization and usage might look like this:

正确的初始化和使用可能如下所示:

EDIT: Changed the example to be a "complete" http server.

编辑:将示例更改为“完整的”http 服务器。

app.js

应用程序.js

var http = require('http');
var express = require('express');

// Requiring express exports a function that creates the application. Call it!
var app = express();

// Set port to listen to
app.set('port', process.env.PORT || 3000);

// Set view engine
app.set('view engine', 'jade');

// Tell express to use the router middleware
// Can be omitted if precedence doesn't matter 
// (e.g. for loading static resources)
app.use(app.router);

// Add callback handler for home (/) route
app.get('/', function(req, res) {
  res.render('index', { title: 'Express' });
});

// Create http server by passing "app" to it:
http.createServer(app).listen(app.get('port'), function() {
  console.log('Express server listening on port ' + app.get('port'));
});

Now, if you place a minimal view into the default folder for views...

现在,如果您将最小视图放入视图的默认文件夹中...

views/index.jade

意见/index.jade

doctype 5
html
  head
    meta(charset='utf-8')
    title #{title}
    meta(name='viewport', content='width=device-width, initial-scale=1.0')
  body
    div
      h1 Gotcha! Title is "#{title}"

... and start your server from the console with...

...并从控制台启动您的服务器...

$ node app.js

...you should have your first node/express/jade powered app up and running!

...您应该启动并运行您的第一个 node/express/jade 驱动的应用程序!

回答by user3624564

Try upgrading to Express 4.x. You are probably running a 3.x flavor.

尝试升级到 Express 4.x。您可能正在运行 3.x 版本。