1const fs = require("fs");
2const hostname = "127.0.0.1";
3const port = 3000;
4
5// use createReadStream instead to save memory
6const index = fs.readFileSync("index.html");
7const java = fs.readFileSync("javascript.js");
8
9// this is our request handler
10const server = http.createServer((req, res) => {
11 if (req.url === "/") {
12 res.setHeader("Content-Type", "text/html");
13 res.write(index);
14 }
15 if (req.url === "/javascript.js") {
16 res.setHeader("Content-Type", "text/javascript");
17 res.write(java);
18 }
19 res.statusCode = 200;
20 // res.writeHead(200, java);
21 res.end();
22});
23
24server.listen(port, hostname, () => {
25 console.log(`Server running at http://${hostname}:${port}/`);
26});