Basics of Express.js
This commit is contained in:
89
Week-05/Day_02/README.md
Normal file
89
Week-05/Day_02/README.md
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
# Express.js Basics
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
This project explains the basics of **Express.js**.
|
||||||
|
Express.js is a web framework built on Node.js that simplifies backend development.
|
||||||
|
|
||||||
|
Express.js is used to create web servers and APIs easily.
|
||||||
|
It provides simple methods for routing and handling HTTP requests.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Features of Express.js
|
||||||
|
|
||||||
|
* Simple and easy routing
|
||||||
|
* Middleware support
|
||||||
|
* Fast server creation
|
||||||
|
* API development
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Example: Simple Express Server
|
||||||
|
|
||||||
|
```js
|
||||||
|
const express = require('express');
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.get('/', (req, res) => {
|
||||||
|
res.send('Hello from Express!');
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(3000, () => {
|
||||||
|
console.log('Server running on port 3000');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Routing Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
app.get('/about', (req, res) => {
|
||||||
|
res.send('This is About Page');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Middleware Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
console.log('Request received');
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## How to Run
|
||||||
|
|
||||||
|
1. Install Node.js
|
||||||
|
2. Install Express:
|
||||||
|
|
||||||
|
```
|
||||||
|
npm init -y
|
||||||
|
npm install express
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Create a file `app.js`
|
||||||
|
4. Run:
|
||||||
|
|
||||||
|
```
|
||||||
|
node app.js
|
||||||
|
```
|
||||||
|
|
||||||
|
5. Open browser:
|
||||||
|
|
||||||
|
```
|
||||||
|
http://localhost:3000
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Technologies Used
|
||||||
|
|
||||||
|
* Node.js
|
||||||
|
* Express.js
|
||||||
|
* JavaScript
|
||||||
Reference in New Issue
Block a user