love
love

Reputation: 1040

Nested routes in express project

I am new to nodejs/express. I have one requirement to host some nested pages. For example, I have to host some pages like:

 http://IP:port/cartoons,
 http://IP:port/cartoons/micky,
 http://IP:port/cartoons/minnie 

I am able to host cartoons page, by creating app.js with below details:

var cartoonRouter = require('./routes/cartoons');
app.use('/cartoons', cartoonRouter);

And making the corresponding changes in routes/cartoon.js And it is working fine. But I am unable to write the same for 'cartoons/micky'.

Could someone help with that?

Upvotes: 0

Views: 1814

Answers (1)

jfriend00
jfriend00

Reputation: 707318

Assuming your /cartoons/micky route is on the cartoonRouter you show in your code, then the router declaration for the micky route should be like this:

router.get('/micky', function(req, res) {
    res.send("got micky");
});

The router itself is registered on /cartoons so any path you put on a route in the router will be added on to the end of /cartoons.

Upvotes: 2

Related Questions