如何根据查询参数不同的html文件

时间:2018-05-27 03:52:53

标签: node.js express

如何根据查询参数显示不同的html文件,当id是狗时,我想显示dog.html,当id为cat时,我想显示cat.html

var app = express();

app.get('/', function(req, res) {
  if(req.query.id = "dog")
    app.send("dog.html");
  else 
    app.sent("cat.html");
});

app.listen(80);

2 个答案:

答案 0 :(得分:1)

这就是你要找的......

app.get('/:id', function(req, res) {
  if(req.params.id === "dog")
    app.send("dog.html");
  else 
    app.send("cat.html");
});

如果您想了解有关参数和路由的更多说明,请阅读文档:

ExpressJS: Routing

答案 1 :(得分:0)

使用sendFile

var path = require('path');
var app = express();

app.get('/', function(req, res) {
  if(req.query.id = "dog")
     res.sendFile(path.join(__dirname + '/dog.html'));
  else 
     res.sendFile(path.join(__dirname + '/cat.html'));
});

app.listen(80);

有关详细信息:https://scotch.io/tutorials/use-expressjs-to-deliver-html-files

https://codeforgeek.com/2015/01/render-html-file-expressjs/