无法使用 http://localhost:8080/test.txt 获取本地文件测试.txt



我有一个服务器在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 来提供它们。

最新更新