Although you can do everything in Node.js with basic javascript, these instructions all assume you are using a middleware package called express. (Almost everyone uses express.)
To do this:const express = require('express'); const app = express(); let port = NUMBER; app.use("/", express.static(__dirname)) app.listen(port, function (error) { if (error) throw error console.log("Server created Successfully") });In the above replace NUMBER with a random number in the range 40000-49999.
node lab6.jsif you get a message about the port already in in use, pick a different number.
http://165.106.10.170:NUMBER/lab5.html http://165.106.10.170:NUMBER/lab6.htmlBoth should work fine.
app.use("/", express.static(__dirname))to
app.use("/", express.static(__dirname+"/public"))
let counter=0 app.use("/time", function (req, res) { timeOfDay(res); }); function timeOfDay(res) { counter++; let date = new Date(); res.write(`<html><body>Counter ${counter}`); res.write(`<br>Date ${date.toLocaleString()}`); res.end("</body></html>"); }This will create a page that contains the current date and time (as the server knows it) and a hit counter.
http://165.106.10.170:NUMBER/time