0

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?

love
  • 1,000
  • 2
  • 16
  • 35
  • weird errors? huh... – Argee Jul 26 '18 at 05:23
  • I mean sometimes "NotFound Error" , ENOENT: no such file or directory etc – love Jul 26 '18 at 05:24
  • Hmm..edited the question..file path is same. But looks like i am unable to write proper code for nested routes :( – love Jul 26 '18 at 05:27
  • 2
    Show us the code you are using for the `/cartoons/micky` route. We can't help you fix code we can't see. Your router should be specifying a route for `/micky` because the router is already installed on `/cartoons`. – jfriend00 Jul 26 '18 at 05:28

1 Answers1

2

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    Sorry for the delay in response. This is the piece of code i was looking for. I was doing a mistake, i had created a new js and a new route "/cartoons/micky" and the was checking with ('/'). Thank you for the help. – love Jul 26 '18 at 06:37