0
我已經有了localhost:8080
運行的服務器,我的文件都按這樣的:無法獲取本地文件test.txt://本地主機:8080/test.txt的
/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
它爲我「不能得到/chatroom.html」
,同樣,如果我去http://localhost:8080/test.txt
它爲我「不能GET /chatroom.html」
我不明白這裏發生了什麼問題,任何幫助將不勝感激!
好的,你能告訴我,我是否理解這一點?我一直在想,顯然不正確的是,在瀏覽器中輸入'http:// localhost:8080/test.txt'與輸入'file:///類似。 。 。/test/test.txt',除了它只是一個相對於'localhost:8080'的路徑。你是說實際上,輸入'http:// localhost:8080/test.txt'向服務器發送一個GET請求,它不知道如何處理? – digglemister
沒錯。當您使用像Apache或Nginx這樣的Web服務器時,通常會將它們配置爲在靜態文件存在的情況下提供服務,並且只有在請求的路徑與文件不匹配時纔會與PHP或node.js等後端連接。但通過連接到端口8080,您直接訪問node&Express,而Express只知道您告訴它的路徑以及您註冊的任何中間件。 –