javascript - Node.js express nested routes - Stack Overflow

I am new to Node.js and Express and have tried to go through some of the tutorials.I am able to get b

I am new to Node.js and Express and have tried to go through some of the tutorials. I am able to get basic routing working one level deep (e.g., http://localhost/help), but I'm having trouble getting it to work two levels deep (e.g., http://localhost/help/test).

Here are the relevant lines in app.js:

var help = require('./routes/help');

// also tried this
//var help_test = require('./routes/help/test');

var app = express();
app.use('/help', help);
app.use('/help/test', help.test);

// also tried this
//app.use('/help/test', test);
//app.use('/help/test', help_test);

Under the routes directory I have two files: index.js and test.js.

The index.js consists of:

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

router.get('/', function(req, res) {
  res.send('help');
});

module.exports = router;

The test.js file consists of:

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


router.get('/test', function(req, res) {
  res.send('help test!');
});

module.exports = router;

Right now I can't start the server due to the configuration in app.js, but any changes I make so that I can start it results in a 404 error when I try to hit http://localhost/help/test

I am new to Node.js and Express and have tried to go through some of the tutorials. I am able to get basic routing working one level deep (e.g., http://localhost/help), but I'm having trouble getting it to work two levels deep (e.g., http://localhost/help/test).

Here are the relevant lines in app.js:

var help = require('./routes/help');

// also tried this
//var help_test = require('./routes/help/test');

var app = express();
app.use('/help', help);
app.use('/help/test', help.test);

// also tried this
//app.use('/help/test', test);
//app.use('/help/test', help_test);

Under the routes directory I have two files: index.js and test.js.

The index.js consists of:

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

router.get('/', function(req, res) {
  res.send('help');
});

module.exports = router;

The test.js file consists of:

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


router.get('/test', function(req, res) {
  res.send('help test!');
});

module.exports = router;

Right now I can't start the server due to the configuration in app.js, but any changes I make so that I can start it results in a 404 error when I try to hit http://localhost/help/test

Share asked Jun 2, 2014 at 20:04 JamesEJamesE 3,9239 gold badges47 silver badges85 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

I think some of your confusion is ing from the require in app.js. Let's look at this line:

var help = require('./routes/help');

That line loads the module in routes/help.js. This file is non-existent In your current configuration. Rename your ./routes/index.js file to ./routes/help.js.

Since the above file will only handle routes prefixed with /help and not /help/test, will need to have an additional require:

var help_test = require('./routes/test');

Your app.js file should now have the following:

var help = require('./routes/index');
var help_test = require('./routes/test');

var app = express();
app.use('/help', help);
app.use('/help/test', help_test);

It should be noted that since your help_test module defines a path at /test, and you "use" it at /help/test, the final path of that route will be: /help/test/test.

Tim's answer got me on the right track, but I ended up with a slightly different solution using his answer that I wanted to post:

In app.js I have:

var help = require('./routes/help');
var help_test = require('./routes/help/test');
....
app.use('/help', help);
app.use('/help/test', help_test);

Under the /routes directory I have this structure:

routes
|-- help
    |-- index.js
    |-- test.js

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744862027a4597759.html

相关推荐

  • javascript - Node.js express nested routes - Stack Overflow

    I am new to Node.js and Express and have tried to go through some of the tutorials.I am able to get b

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信