简体   繁体   English

Node.js 提供“/ 索引”

[英]Node.js serves "index of /"

I've been experimenting with Node.js on my windows machine and also my linux machine to create a web server using node.我一直在我的 Windows 机器和我的 linux 机器上尝试使用 Node.js 来创建一个使用 node.js 的 Web 服务器。

If I try my code on windows and activate my server with "node server.js" and navigate to localhost:8080 I am met with "Hello World!"如果我在 Windows 上尝试我的代码并使用“node server.js”激活我的服务器并导航到 localhost:8080 我会遇到“Hello World!” as intended.如预期。

But if I try on my linux box with the same code it shows me the directory instead of serving the page.但是,如果我使用相同的代码在我的 linux 机器上尝试,它会向我显示目录而不是提供页面。 Not sure what's going on here!不知道这里发生了什么!

Here's the code I've been using这是我一直在使用的代码

var http = require('http');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');

I've tried using my local ip to the machine and also using the domain followed by ":8080", both give me the same results.我试过使用我的本地 ip 连接到机器,并使用后跟“:8080”的域,两者都给我相同的结果。

I cant find anybody else with the same problem and I've been looking for hours.我找不到其他有同样问题的人,我已经找了好几个小时了。

指数

I got the same issue while manually starting server from windows machine.我在从 Windows 机器手动启动服务器时遇到了同样的问题。 using command http-server -p 4200使用命令 http-server -p 4200

Say, I was trying to start the server from D:/ProjectName.说,我试图从 D:/ProjectName 启动服务器。 When I changed that and started from D:/ProjectName/build, it was working fine.当我改变它并从 D:/ProjectName/build 开始时,它工作正常。 Build folder has the index file and all other files.构建文件夹包含索引文件和所有其他文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM