我想創建一個nodejs服務器 - 我以前設法 - 沒有HTML頁面。不過,爲了這個任務,我需要將它直接指向RedGreenBlue.html。我真的很掙扎,任何建議將不勝感激。鏈接HTML頁面到NodeJS服務器 - 不會加載爲本地主機?
非常感謝
JS服務器
var http = require('http'),
fs = require('fs');
fs.readFile('./RedGreenBlue.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
});
您能描述一下當前行爲與預期行爲之間的關係嗎?目前還不清楚你的代碼是否適合你。 – Jacob
對不起 - 我輸入'node ServerRedBlueGreen.js'到nodejs控制檯並沒有任何反應,因爲沒有發生任何錯誤。當我嘗試加載本地主機時,我只是收到一條錯誤消息,說它拒絕響應。 – Anniee
這對我有用(提供'RedGreenBlue.html'實際存在)。你確定它確實嗎? – Jacob