如何根据查询参数显示不同的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);
答案 0 :(得分:1)
这就是你要找的......
app.get('/:id', function(req, res) {
if(req.params.id === "dog")
app.send("dog.html");
else
app.send("cat.html");
});
如果您想了解有关参数和路由的更多说明,请阅读文档:
答案 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