0
我自學的Node.js.我創建了兩個簡單的HTML文件(summer.html和winter.html),並在node.js上節點了JS。我繼續使用localhost:5354/summer.html(和winter.html)。沒有什麼是顯示出來,我得到了我已經測試過其他課程的錯誤消息解析查詢字符串不工作
This site can’t be reached
The connection was reset.
Try:
Checking the connection
Checking the proxy and the firewall
Running Windows Network Diagnostics
ERR_CONNECTION_RESET
,並能夠在本地主機上顯示的結果:5354 /,但一個不工作。我做錯了什麼?
JS
var http = require('http');
var url = require('url');
var fs = require('fs');
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
fs.readFile(filename, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(5343);
你需要在你的server.js(或main.js,主要後端js文件)中處理路由,或者將html文件放在與index.html相關的地方 –
你正在監聽端口5343,但在瀏覽器中使用5354. – alexmac
通常使用一個簡單的端口,例如3000。 – alexmac