Is it possible to put variables in routes in Express?

It’s the title.

Is it possible to put variables in routes in Express?

You need to elaborate further. Nobody can help you without assumptions unless you provide a proper description of the problem.

1 Like

I guess you mean dynamic routes, so for instance, premake a /articles/{id}.
You can do that like so:

// Put the dynamic part after a colon (:).
app.get('/article/:id', function(req , res){
  res.render('article' + req.params.id);
  // Instead of .id put whatever you put in the dynamic part
});

Edit: source javascript - How to configure dynamic routes with express.js - Stack Overflow

4 Likes

Adding on to @Nicolello’s answer, if the route itself should be variable:

app.get(`/${variable}`, function(req, res) {
    res.render("some_secret_page.html");
})
3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.