這實在是很奇怪的問題。我剛剛在我的系統上安裝了Node.JS(Fedora)。內部腳本和樣式表始終指向索引文件(Node.JS)
我在在/ var/WWW /鏡像/三個文件:
- server.js
- client.js
- 的index.html
文件服務器。 js是我通過CLI調用的那個:node server.js
。
它基本上返回index.html。
var
http = require('http'),
io = require('socket.io'),
fs = require('fs');
http.createServer(function(request, response) {
fs.readFile(__dirname + '/index.html', function(error, data) {
if (error) {
result.writeHead(500);
console.log('Error: Could not read index.html.');
}
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(data);
});
}).listen(1337, '127.0.0.1');
console.log('Server is running.');
所有按預期工作,沒有任何錯誤引發任何地方。
在的index.html我有簡單的HTML5結構(沒有不必要的,真的!),並<script />
指向,已經提到,client.js。
這行代碼看起來是這樣的(Ctrl + U鍵;從瀏覽器):
<script src="client.js"></script>
通過在client.js
移動光標,我得到實際位置:http://127.0.0.1:1337/client.js
。
似乎是對的,對不對?
問題:
通過打開它打開想要的文件的鏈接,但內容是server.js
應返回。
這不允許我包含任何內部腳本和樣式表!
我想,任何通過http://127.0.0.1:1337/
(也http://127.0.0.1:1337/client.js
,http://127.0.0.1:1337/a/b/c
等)變爲通過server.js處理 - 和server.js回報的index.html(見上文)。
我該如何解決?謝謝你的任何建議!
這裏沒有什麼奇怪的。 'FS。readFile(__ dirname +'/index.html',...'在代碼中,它只是做你想做的事 - 爲任何請求提供index.html。 – 2011-12-04 18:39:11