在node.js

时间:2015-10-11 15:38:33

标签: javascript node.js

我有一系列人如下:

people = ['personA', 'personB', 'personC'];

我希望能够根据网址显示有关此人的网页。例如,localhost:3000/people/personA会引导我访问有关personA的页面。

如何为每个团队成员多次指定以下内容?有没有办法可以在' / people /'?

之后抓住这个部分
  app.get('/people/personA', (req, res) => {
     // render view for personA
  }
  });

2 个答案:

答案 0 :(得分:1)

您应该使用网址中的参数进行操作。您可以使用:param_here 添加参数,并使用req.params.param_here获取其值,如下所示:

app.get('/people/:person', (req, res) => {
    var person = req.params.person; // Variable telling you which person to show
  }
});

答案 1 :(得分:0)

这是解决方案

app.get('/people/:person', funcation (req, res){
    res.render("views/"+req.params.person+".html"); 
  }
});
相关问题