Javascript Node Express Res SendFile
我在res.send上下文中遇到问题。我的目录中确实有Javascript Node Express Res SendFile,javascript,node.js,express,Javascript,Node.js,Express,我在res.send上下文中遇到问题。我的目录中确实有index.html,但它并没有像我期望的那样工作。它只是不显示我的HTML文件 app.listen(8081, function(){ console.log("Servidor rodando na url http://localhost:8081"); }); app.get("/sendfile", function(req,res){ res.send("Página SENDFILE") res.se
index.html
,但它并没有像我期望的那样工作。它只是不显示我的HTML文件
app.listen(8081, function(){
console.log("Servidor rodando na url http://localhost:8081");
});
app.get("/sendfile", function(req,res){
res.send("Página SENDFILE")
res.sendFile(__dirname + "index.html");
});
以下是localhost页面中显示的内容:
这是我的目录:
首先,它应该产生
标题发送后无法设置
因为一旦收到以下人员的回复:
res.send('Pagina SendFile')
已发送,但不会发送index.html
文件
首先应按如下方式设置视图引擎:
app.set('view engine','html')在server.js
文件中
最重要的是,您应该在视图中保留所有HTML
文件,然后设置视图引擎并最终使用以下命令发送:
res.status(200).sendFile(\uu dirname+'index.html')
您的问题是res.send()
立即发送响应,您只能对一个请求发送一个响应,因此sendFile()
被忽略。您不能在一个响应内进行多次发送。。您可以发送一些文本,也可以发送一个文件,而不是两者都发送。另外->\uu dirname+“index.html”
使用path.join(\uu dirname,“index.html”)
。。或者您将尝试发送文件,如->c:\appdirindex.html
。aka没有路径分隔符。我尝试了,但是:app.get(“/sendfile”,function(req,res){res.sendfile(path.join(u dirname,“index.html”);})代码>这是web打开时显示的内容:引用错误:路径未在c:\Users\Edgelson\Desktop\web\Node\Freespace\Node\U 470af21675bfd处定义。tmp:27:18嘿,谢谢!它起作用了。如果有人需要,我将把代码放在这里:app.get(“/sendfile”,function(req,res){app.set('view engine','html');res.status(200).sendfile(uu dirname+'/index.html');})代码>