0

I very much new to Node JS and Express Framework & mongodb. I am working on a website which has multiple categories and when we click on category it goes to a view, where based on the category clicked, data gets loaded in the view.

please check the link for website here.

I am looking help for navbar links eg. Montessori Premium->Toddler Material

As these are static links I can not figure how to pass any parameter or url format for this.

I have done the routing for individual products as they have ids coming from database so I am using that id in url.

Thanks in advance.

2 Answers2

1

It seems to me like you're just missing a bit of glue in between the front-end and the back-end database calls. Here's a rough idea of the sequence you might be after and an explanation of what I think you're missing.

If I'm wrong, please add a comment and update your question to include the new details and I'll see about editing my answer.

In this example I'm creating a website which has a list of "items" in a shop and we're looking to display them all on a single page. Each item should then have an a href clicking through to that specific item where there might be more detail such as a "full description" of the item and maybe some images or something. We're going to look at the flow to build that first page with the list of items.

  1. The user makes a HTTP GET call to the root of your website such as "http://www.mystore.com/".
  2. This request comes through in Express to your controller which takes the req, res, next parameters.
  3. Your controller makes a call to the database to get a list of all items including their names and ID's.
  4. You then use something called templating to inject in the list of items.
  5. In this template you iterate through the list doing something like this.

.

<ul>
    {{#each items}}
        <li><a href="/items/{{id}}>{{name}}</a></li>
    {{/each}}
</ul>

In this template (which happens to be Handlebars), we're going through the list of items and using the id and name of each to inject them into the HTML to get something a bit like this back.

<ul>
    <li><a href="/items/1>Shoes</a></li>
    <li><a href="/items/2>Red spotted dress</a></li>
    <li><a href="/items/3>Jeans</a></li>
</ul>
  1. This is then served up to the user once the template has been processed.

In essence I think you're after some sort of templating engine, I'd highly recommend having a look at Handlebars with something like the express-hbs.

Elliot Blackburn
  • 3,759
  • 1
  • 23
  • 42
0

Thank you @Elliot for your time to answer this. Yes I am using handlebars for templating. The solution I found is, because I am using static links, I am passing query string in anchor tag link. eg. http://localhost:8000/product?category=Toddler_Material and then using req.query for capturing the values (Toddler_Material) in router

var category = req.query.category;