Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.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 VS代码显示列表目录,而不是提供html文件_Javascript_Html_Node.js_Express_Web Development Server - Fatal编程技术网

Javascript VS代码显示列表目录,而不是提供html文件

Javascript VS代码显示列表目录,而不是提供html文件,javascript,html,node.js,express,web-development-server,Javascript,Html,Node.js,Express,Web Development Server,我想通过nodejs提供Disp.html文件,遵循通过Youtube教程学习的代码 consthttp=require(“http”); 常数fs=要求(“fs”); const fileContent=fs.readFileSync(“Disp.html”); const server=http.createServer((req,res)=>{ res.writeHead(200,{“内容类型”:“text/html”}); res.end(文件内容); }); 听(80,“127.0.0

我想通过nodejs提供Disp.html文件,遵循通过Youtube教程学习的代码

consthttp=require(“http”);
常数fs=要求(“fs”);
const fileContent=fs.readFileSync(“Disp.html”);
const server=http.createServer((req,res)=>{
res.writeHead(200,{“内容类型”:“text/html”});
res.end(文件内容);
});
听(80,“127.0.0.1”,()=>{
日志(“在端口80上侦听”);
});
我在VS代码中创建了一个文件夹,文件为app.js和Disp.html。每次运行代码时,它都会显示列表目录,而不是我希望查看的HTML内容


我该怎么做才能得到预期的结果呢?

谢谢,我知道了。本地主机位于端口5500而不是80,在代码中更改该端口使我获得了提供的html文件


所以是服务器。听着(5500,…)

您运行它的方式有很大问题。听起来您可能正在运行VS Code live server并访问该服务器,而不是运行上面代码中编写的服务器。似乎您在www目录外获得了此文件,启动节点应用程序并在公用目录中拥有正确的文件,请注意,直接处理
http.createServer
几乎总是比您的时间价值要烦人得多,而且您通常最好使用Express.js。@PatricNox-不。没有
www
目录,因为这不是Apache/IIS/什么的。如果
Disp.html
位于错误的位置,则尝试读取时会出错。