无法使用 http://localhost:8080/test.txt 获取本地文件 test.txt
Can't get local file test.txt using http://localhost:8080/test.txt
我在 localhost:8080
上有一个服务器 运行,我的文件是这样排列的:
/test
server.js
chatroom.html
test.txt
server.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
时它显示 "Cannot GET /chatroom.html"
同样,如果我转到 http://localhost:8080/test.txt
,它会显示 "Cannot GET /chatroom.html"
我不明白这里出了什么问题,如有任何帮助,我们将不胜感激!
您只为 Express 定义了一个路径,即根路径“/”。如果您想要其他文件可用,您也需要为它们调用 app.get()。
或者,您可以将这些静态资产放在一个目录中,然后 use express.static 为它们提供服务。
我在 localhost:8080
上有一个服务器 运行,我的文件是这样排列的:
/test
server.js
chatroom.html
test.txt
server.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
时它显示 "Cannot GET /chatroom.html"
同样,如果我转到 http://localhost:8080/test.txt
,它会显示 "Cannot GET /chatroom.html"
我不明白这里出了什么问题,如有任何帮助,我们将不胜感激!
您只为 Express 定义了一个路径,即根路径“/”。如果您想要其他文件可用,您也需要为它们调用 app.get()。
或者,您可以将这些静态资产放在一个目录中,然后 use express.static 为它们提供服务。