gpt4 book ai didi

node.js - 如何使用 app.get 路由到 Nodejs 和 Express 上的新页面

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:24 24 4
gpt4 key购买 nike

我一直在尝试使用 app.get 路由到新的 html 页面,如下所示:例如当我输入 localhost:3000/newpage 时,我希望它路由到 newpage.html

我只能通过 app.get 路由到另一个 JS 文件。是否可以做到这一点,但改为路由到 html 文件?如果没有,是否有更合适的方法来做到这一点?我是 Nodejs 新手,所以任何帮助都会有帮助!

我目前拥有的应用程序.js

var graball = require('./public/javascripts/graball')
app.get('/graball', function (req, res) {
res.send(ibm);
});

我想要什么应用程序.js

var page = require('./public/page1'); //page1.html
app.get('/page1', function(req, res) {
res.send(page1);
}

最佳答案

您可以使用sendFile :

app.get('/sitemap',function(req,res){
res.sendFile('/sitemap.html');
});

关于node.js - 如何使用 app.get 路由到 Nodejs 和 Express 上的新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33237595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com