我有一个服务器在localhost:8080
上运行,我的文件排列方式如下:
/test
server.js
chatroom.html
test.txt
服务器.js:
var express = require("express");
var app = express();
app.get("/", function(request, response) {
response.sendFile(__dirname + "/chatroom.html");
});
app.listen(8080);
因此,当我转到http://localhost:8080
时,我的浏览器中chatroom.html
服务很好。
我遇到的问题:
但是当我去http://localhost:8080/chatroom.html
时,它显示"无法获取/聊天室.html"
而且,同样,如果我去http://localhost:8080/test.txt
它会显示"无法获取/聊天室.html"
我不明白这里出了什么问题,任何帮助将不胜感激!
您只为 Express 定义了一条路径,即根路径"/"。如果你想要其他文件可用,你也需要为它们调用app.get()。
或者,您可以将这些静态资产放在目录中,并使用 express.static 来提供它们。