Server static files using Express.js, we have created a directory with the name of public and express will automatically server all the files in the folder. We are going to server static file using app.use(express.static(‘public’)). This is know as middleware in express.

When we call root directory then express static middlerware automatically called public folder and it server index.html file from it.

serverstatic.js

var express = require('express');
var app = express();
var path = require('path');

app.use(express.static(path.join(__dirname, 'public')));

app.listen(3000, function(){
    console.log("Magic happens now at port 3000");
});

Public folder

-public
--css
--images
--javascript
--index.html

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>This is HTML File</title>
    <link rel="stylesheet" href="./css/style.css">
</head>
<body>
    <h1>This is html Page</h1>
</body>
</html>

style.css

Get More Knowledge: