Node.js express nested routes

JamesE

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

Tim Cooper

I think some of your confusion is coming 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Node.js express nested routes

From Dev

Node.js + express: specify routes

From Dev

Redirecting routes in Node using Express and Passport JS

From Dev

Nested routes in express project

From Dev

Node.js Express nested resources

From Dev

node.js express nested callback in route

From Dev

node.js express routes using new RegExp

From Dev

How to separate routes on Node.js and Express 4?

From Dev

Node.js and express Rest api to create custom fields routes

From Dev

Routes in express JS

From Dev

Express JS Routes

From Dev

Emulating Node/Express Routes in .htaccess

From Dev

Express.js dynamic routes

From Dev

Express js routes - overlapping params

From Dev

Express.js routes with Typescript

From Dev

Consolidating Routes in Express.js

From Dev

Are the variables defined in app.js accessible to the functions in routes, in Express and node js?

From Dev

Are the variables defined in app.js accessible to the functions in routes, in Express and node js?

From Dev

What is the DRY approach for Nested Express Routes

From Dev

Nested routes in Ember.js

From Dev

Node.js, Express, Redis - Routes from separate modules cannot be called

From Dev

Node.js & Express: Static assets on dynamic routes aren't found

From Dev

Node Express.js access variables from routes declared in separate files

From Dev

Node.js express routes aren't catching some GETS even with wildcard

From Dev

How to get different css files for different routes (express, node.js)

From Dev

Node.js, Express, Redis - Routes from separate modules cannot be called

From Dev

Node js express can't parse body after routes moved into own folder

From Dev

Bootstrap theme's assets management in node.js project with express routes

From Dev

Node Express 4 middleware after routes

Related Related

  1. 1

    Node.js express nested routes

  2. 2

    Node.js + express: specify routes

  3. 3

    Redirecting routes in Node using Express and Passport JS

  4. 4

    Nested routes in express project

  5. 5

    Node.js Express nested resources

  6. 6

    node.js express nested callback in route

  7. 7

    node.js express routes using new RegExp

  8. 8

    How to separate routes on Node.js and Express 4?

  9. 9

    Node.js and express Rest api to create custom fields routes

  10. 10

    Routes in express JS

  11. 11

    Express JS Routes

  12. 12

    Emulating Node/Express Routes in .htaccess

  13. 13

    Express.js dynamic routes

  14. 14

    Express js routes - overlapping params

  15. 15

    Express.js routes with Typescript

  16. 16

    Consolidating Routes in Express.js

  17. 17

    Are the variables defined in app.js accessible to the functions in routes, in Express and node js?

  18. 18

    Are the variables defined in app.js accessible to the functions in routes, in Express and node js?

  19. 19

    What is the DRY approach for Nested Express Routes

  20. 20

    Nested routes in Ember.js

  21. 21

    Node.js, Express, Redis - Routes from separate modules cannot be called

  22. 22

    Node.js & Express: Static assets on dynamic routes aren't found

  23. 23

    Node Express.js access variables from routes declared in separate files

  24. 24

    Node.js express routes aren't catching some GETS even with wildcard

  25. 25

    How to get different css files for different routes (express, node.js)

  26. 26

    Node.js, Express, Redis - Routes from separate modules cannot be called

  27. 27

    Node js express can't parse body after routes moved into own folder

  28. 28

    Bootstrap theme's assets management in node.js project with express routes

  29. 29

    Node Express 4 middleware after routes

HotTag

Archive