Introduction to Express.JS
1.1 What is Express.JS
Express.js is a minimal and flexible web application framework for Node.js. It provides a robust set of features to develop web and mobile applications.
Think of Express as the engine that helps your Node.js server easily handle HTTP requests, routing, middleware, and much more.
Think of Express as the engine that helps your Node.js server easily handle HTTP requests, routing, middleware, and much more.
1.2 Why use Express.JS
- Simplifies server creation in Node.js.
- Supports routing, middleware, and templating.
- Ideal for building RESTful APIs or MVC apps.
- Works well with databases like MongoDB, MySQL, etc.
- Supports routing, middleware, and templating.
- Ideal for building RESTful APIs or MVC apps.
- Works well with databases like MongoDB, MySQL, etc.
1.3 Installing Express.JS
NOTE: Make sure Node.JS is installed in your system.
STEP 1 - Create a folder for your app:
STEP 2 - Initialize a Node.js project:
STEP 3 - Install Express:
Now Express.JS has been loaded in your app
STEP 1 - Create a folder for your app:
bash
1mkdir express-intro
2cd express-intro
STEP 2 - Initialize a Node.js project:
bash
1npm init -y
STEP 3 - Install Express:
bash
1npm install express
Now Express.JS has been loaded in your app
1.4 First Express App
Create a file called app.js and add:
and then run it using:
Visit http://localhost:3000 — you will see your welcome message!
app.js
1const express = require('express');
2const app = express();
3
4app.get('/', (req, res) => {
5 res.send('Hello, User! Welcome to Express.js!');
6});
7
8const PORT = 3000;
9app.listen(PORT, () => {
10 console.log(`Server is running at http://localhost:${PORT}`);
11});
and then run it using:
bash
1node app.js
Visit http://localhost:3000 — you will see your welcome message!