I'm tring to create a simple web server to run my local html file, and now I can get a server running on localhost using code below
var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('index.html', 'utf8');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'html'});
res.end(index);
}).listen(3000);
But there is a problem that every time I changed my html file, the web page on localhost is still the old one, I need to restart the server to make the changes visible.
I want to improve that and I tried to use koa or express, like code below
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("Application started and Listening on port 3000");
});
app.get("/", (req, res) => {
res.sendFile("test.html");
});
And I found that express can serve real-time html files, I don't need to restart serve when files change.
How can I use http moudle to achieve that?