Creating a Simple Static Web Server with Express.js
By Deshan Nawanjana
•
Install express package.
npm i express
Create a file for the server script server.js and import express module.
Then create an express app and use express.static to set the static file
serve options. You can provide a relative path or an absolve path and specify which directory should be the root of
the server. To change the port number, provide a different one to app.listen method.
// import express module
import express from "express"
// create express app
const app = express()
// set static file serve options
app.use(express.static("D:/path/to/root"))
// start server with port
app.listen(8080, () => {
// display server started message
console.log("Static Server Started: http://localhost:8080")
})
Start server from command prompt.
node server.js
Open your web browser and visit the static server.
http://localhost:8080