4

I have an SPA made with express-handlebars. I need to pass route parameters in the URL so I can pick it up in the subsequent page it leads to and render the details pertaining to that parameter. for eg.

www.sitename.com/events/1 , www.sitename.com/events/2 1 and 2 will be the event IDs which I will then use to fetch the details of that event.

I need handlebars to render the same eventdetail page for me for both routes as shown above. But it seems to break everything, and the console check showed me that it was trying to go inside a "events" folder and then trying to find all the files within and eventually throwing a 404 page as well.

These are the routes I have right now in my routes.js page.

router.get("/events", function(req, res, next) {
  res.render("events");
});
router.get("/eventdetail", function(req, res, next) {
  res.render("eventdetail");
});

How do I go about with this?

Gilles Heinesch
  • 2,889
  • 1
  • 22
  • 43
Scary Terry
  • 217
  • 3
  • 14

1 Answers1

0

You have to modify your routes to handle path param passed in url. Currently, express is looking for path which matches events/1 which is not exists in you configuration hence it's throwing 404 error.

Change your /events route to use path param like below

router.get("/events/:page", function(req, res, next) {
    // you can access page value here with 
    // req.params.page
    // for /events/1, req.params.page will return value "1"
    res.render("events");
});```
Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
  • 1
    I did try this before I posted here, but this lead to the entire website breaking because now the server would try to look for all dependencies (all js and css files) in `www.sitename/events/1` folder structure. Anyway, i passed the event id as a parameter in the url (www.sitename.com/eventdetail?id=1) cause that seemed to be the simplest, least complicated way of doing it. – Scary Terry Mar 05 '19 at 04:57