简体   繁体   English

在Express和Node.js中使用路由器

[英]Using a router with Express and Node.js

I'm new to Node.js. 我是Node.js的新手。 I've pulled some code from examples, but somehow I've broken something :). 我已经从示例中提取了一些代码,但是不知何故,我弄坏了一些东西:)。

At this time, in my app.js file, I have a line that I think wires up Express with Node.js. 这时,在我的app.js文件中,我有一行连接到Express。和Node.js。 That line looks like this: 该行如下所示:

app.js app.js

var routes = require('./routes/index');
// ...
app.get('/', routes.router);

Then, in ./routes/index.js I have the following: 然后,在./routes/index.js中,我有以下内容:

routes/index.js 路线/index.js

var express = require('express');
var router = express.Router();

/* GET home page */
router.get('/', function(req, res) {
    res.send('respond with a resource');
});
module.exports = router;

When I run this, I get the following error: 运行此命令时,出现以下错误:

Error: Route.get() requires callback functions but got a [object Undefined]
    at Route.(anonymous function) [as get]

I don't understand. 我不明白 What am I doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

app.js app.js

var routes = require('./routes/index');
//var routes = require('./routes') --> this works 
// ...
app.use('/', routes); //Using the router instance as a middleware , relative to '/'

routes/index.js 路线/index.js

var express = require('express');
var router = express.Router(); // new instance of Router

/* GET home page */
router.get('/', function(req, res) {
    res.send('respond with a resource');
});
module.exports = router; // You export the intance

UPDATE if you want more than 1 route file 如果你想超过1条路线文件更新

app.js app.js

var routes = require('./routes')
app
 .use("/user",routes.user)
 .use("/other",routes.other)

routes/index.js 路线/index.js

module.exports = {
  user : require(./user),
  other : require(./other)
}

routes/user.js 路线/user.js

var router = require("express").Router()

router.get("/",function (req,res){
  // GET /user
})
.post("/",function (req,res){
  //POST /user
})

module.exports = router;

routes/other.js 路线/other.js

var router = require("express").Router()

router.get("/",function (req,res){
  // GET /other
})
.post("/",function (req,res){
  //POST /other
})

module.exports = router;

An example of a basic server.js: 基本server.js的示例:

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

app.get('/', function(req, res) {
    res.sendfile(__dirname + '/client/views/index.html');
});

app.listen(3000, function() {
    console.log('Server running on localhost:3000');
});

Comparing, I believe you need to listen to the port. 相比之下,我相信您需要听听端口。 Also I think your res.send should be sending an actual file. 我也认为您的res.send应该发送一个实际的文件。

More documentation on Nodejs here: https://nodejs.org/api/ 有关Nodejs的更多文档,请访问: https ://nodejs.org/api/

Also found a related questions on SO: Node Route.get() requires callback function but got a [object undefined] 还发现了一个与SO相关的问题: Node Route.get()需要回调函数,但是得到了[object undefined]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM