我的測試文件中有index.html
和server.js
。 我的server.js包含;Node.js服務器不重新刷新我的index.html
const http=require('http');
const fs=require('fs');
const hostname='127.0.0.1';// or we can type localhost
const port=3000;
fs.readFile('index.html', (err,html) => {
if(err) {
throw err;
}
const server=http.createServer(function(req, res){
res.statusCode=200;
//res.setHeader('Content-type',"text/plain");
res.setHeader('Content-type', "text/html");
res.write(html)
//res.end("Hello world."); if we dond use html, we see hello wordl
res.end();
});
server.listen(port,hostname,() => {
console.log("Server started on port "+ port);
});
});
我加入到我的index.html <p>Lorem ıpsum dolor sit</p>
和運行server.It工作。
之後,我加<h2>Lorem2</h2>
然後打C:\test>node server
並重新啓動服務器,html沒有刷新。
爲什麼會發生這種情況? (我也嘗試換端口)
在此先感謝。
*「...並重新啓動服務器,html沒有刷新。」*這告訴你**瀏覽器**緩存它。 –
那我該怎麼辦? @ T.J.Crowder – Norx