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.


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



<!DOCTYPE html>
<html lang="en">
    <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">
    <h1>This is html Page</h1>


Get More Knowledge: