Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct/Concisest way to nest routers with Express

I can set up two routes like this

index.js

var express = require('express');
var app = express();
var router = express.Router();
const PORT = 3001;

app.get('/', function(req, res){
    res.send('hello app');
});

app.use('/routes', require('./routes'));

app.listen(PORT, function(){
    console.log('listening on port:', PORT);
});

./routes/index.js

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

router.use('/sub1', require('./sub1'));
router.use('/sub2', require('./sub2'));

module.exports = router;

./routes/sub1.js

var express = require('express');
var app = express();
var subOneRouter = express.Router();

subOneRouter.get('/', function(req, res){
    res.json({route: 'sub1-base'});
});

subOneRouter.get('/:id', function(req, res){
    res.json({'route': 'sub1-base', 'id': req.params.id});
});

module.exports = subOneRouter;

For brevity ./routes/sub2.js looks exactly the same, but its variables are named subTwo

What is the shortest way to nest sub2 under sub1? Within index.js I have tried

var subOne = router.use('/sub1', require('./sub1'));
subOne.use('/sub2', require('./sub2'));

But that didn't work at all. Within index.js

router.use('/sub1/:id/sub2', require('./sub2'));
//localhost:3000/sub1/123/sub2/456 => { "route": "sub2-base","id":"456"}

Does work, but it seems it could get verbose and difficult to maintain if the structure got much longer. What's the best way to do this? Is there a shorter way to nest these?

like image 895
1252748 Avatar asked Sep 29 '16 00:09

1252748


People also ask

What is the correct order of defining routing using Express methods are?

The order is first come first serve. In your case, if user hits /api, he will get response from api, but if you write /:name route before /api , /:name will serve for /api requests also.

What is express routing?

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on). Each route can have one or more handler functions, which are executed when the route is matched.

What does router Express do?

Route functions The "about" route (reproduced below) is defined using the Router. get() method, which responds only to HTTP GET requests. The first argument to this method is the URL path while the second is a callback function that will be invoked if an HTTP GET request with the path is received.


1 Answers

Your code in index.js makes it difficult to understand what you want. So far I understand you want a route like /sub1/:id/sub2 but more easy to write and maintain and inside index.js.

So yes you can do it and it is quite simple. You just need to require sub1 and sub2 and use sub2 in sub1, then you can mount sub1 on the router. Ex:

var sub1= require('./sub1');
var sub2 = require('./sub2');
sub1.use(sub2);
router.use('/sub1:id', sub1);

So your index.js becomes,

var express = require('express');
var app = express();
var router = express.Router();
const PORT = 3001;

app.get('/', function(req, res){
    res.send('hello app');
});

var sub1= require('./sub1');
var sub2 = require('./sub2');
sub1.use(sub2);

router.use('/sub1:id', sub1);

app.listen(PORT, function(){
    console.log('listening on port:', PORT);
});

This wouldn't be very difficult to maintain. Let me know if this isn't what you are looking for.

like image 75
Naeem Shaikh Avatar answered Oct 22 '22 23:10

Naeem Shaikh