Nearby lessons

11 of 14

🎨 How To Install and Use Bootstrap in Express JS and Node JS

πŸ“Œ What is Bootstrap?

πŸ’‘ What Is Bootstrap?

Bootstrap is most popular CSS framework used to build responsive and mobile-first websites.

Bootstrap 5 is the latest version and includes powerful classes for layout, design, grid system, and components like buttons, cards, navbars, alerts, forms, and much more.

βš™οΈ Why Use Bootstrap in Express.js?

βš™οΈ Why Use Bootstrap in Express.js?

Using Bootstrap with Express.js allows you to build attractive UI pages quickly inside EJS templates without writing CSS from scratch.

πŸ“¦ Step 1: Install Bootstrap

Use npm to install Bootstrap into your Node.js + Express project:

npm i bootstrap

This command downloads the Bootstrap files inside the node_modules folder.

πŸ“ Step 2: Project Folder Structure

Create the folders and EJS files like this:

πŸ“ project-folder
 ┣ πŸ“ node_modules
 ┣ πŸ“ views
 ┃ ┣ πŸ“ common
 ┃ ┃ ┣ πŸ“„ header.ejs
 ┃ ┃ β”— πŸ“„ footer.ejs
 ┃ ┣ πŸ“„ home.ejs
 ┃ ┣ πŸ“„ about.ejs
 ┃ β”— πŸ“„ contact.ejs
 ┣ πŸ“„ package.json
 β”— πŸ“„ index.js
        

🧱 Step 3: Setup Bootstrap in Express.js

We link Bootstrap CSS and JS files by exposing them as static files from node_modules.

Express.js Server
NODE RUNTIME
const express = require('express');
const path = require('path');
const cors = require('cors');
const app = express();

app.use(cors());
app.set('view engine', 'ejs');

// Routes
app.get('/', (req, res) => {
    res.render('home');
});

app.get('/about', (req, res) => {
    res.render('about');
});

app.get('/contact', (req, res) => {
    res.render('contact');
});

app.listen(8080, () => {
    console.log("Server running at http://localhost:8080");
});

Browser Output
Server logs

πŸ“˜ How It Works

  • πŸ“¦ npm i bootstrap installs Bootstrap into your project.
  • πŸ›£οΈ express.static() exposes CSS & JS files from node_modules.
  • 🎨 You include Bootstrap in EJS using <link> and <script> tags.
  • πŸ“„ Each EJS page can now use Bootstrap classes like bg-success, mt-5, btn btn-primary, etc.
  • 🧱 Header and footer are included dynamically using EJS partials.

🧠 Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!Middleware in Express.js