Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node Express Res SendFile_Javascript_Node.js_Express - Fatal编程技术网

Javascript Node Express Res SendFile

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

我在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.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');})