Posts

Showing posts with the label static

Use Express Server to Deliver HTML Files quickly

Image
Create a folder ex. "MyProject" Use command npm init for initiating package.json Once setup, install Express module using npm i express --save Open the folder using VS Code or any other IDE, create a file called server.js (  You may use any name  ) Copy paste this below code : const express = require( 'express' ); const app = express(); const path = require( 'path' ); const port = process.env.PORT || 3000 ; app.get( '/' , function (req, res) { res.sendFile(path.join(__dirname + '/index.html' )); }); app.listen(port,() => { console.log( ` started on port ${port} ` ); }); You may create multiple route as per your need, use command  node server.js  to start the server For quick access go to package.json file, and add an entry to scripts category like below example : Use ctrl + c to stop the server. { "name" : "image" , ...