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
π How It Works
- π¦
npm i bootstrapinstalls Bootstrap into your project. - π£οΈ
express.static()exposes CSS & JS files fromnode_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 QuestionsProgress: 0 / 5
Keep Going!Middleware in Express.js