How to configure dynamic routes with express.js

前端 未结 7 2394
Happy的楠姐
Happy的楠姐 2020-12-07 20:32

I have a route.js which looks like this:

module.exports = function(app) {

  app.get(\'/tip\', function(req, res) {
    res.render(\"tip\");
  });

  app.get         


        
7条回答
  •  半阙折子戏
    2020-12-07 20:37

    I would do the same thing you did for /modules/:name

    app.get('/article/:id', function(req , res){
      res.render('article' + req.params.id);
    });
    

    It would be more meaningful from a rest point of view.

    If you cannot do it for any particular reason you might want to do something like:

    var articlesEndpoints = ['/article2', '/article3'];
    articlesEndpoints.forEach(function(name) {
      app.get(name, function(req, res) {
        res.render(name);
      });
    });
    

    Is this what you meant?

提交回复
热议问题