Express.
js Project Setup in VS Code
### Step 1: Install VS Code
1. Download and install VS Code from: [Link]
### Step 2: Install [Link]
1. Download and install [Link] from: [Link]
2. Verify installation:
```sh
node -v
npm -v
```
### Step 3: Create a New Project
1. Open VS Code, go to **File > Open Folder**, and create a folder **expressjs**.
2. Open the terminal (`Ctrl + ~`) and run:
```sh
mkdir expressjs
cd expressjs
```
### Step 4: Initialize a [Link] Project
```sh
npm init -y
```
### Step 5: Install Dependencies
```sh
npm install express
npm install -D nodemon
```
### Step 6: Create `[Link]`
1. In **VS Code**, create a file named **[Link]** inside **expressjs**.
2. Copy and paste this code:
const express = require('express');
const app = express();
[Link]([Link]());
const products = [
{ id: 1, name: "mi" },
{ id: 2, name: "iphone" },
{ id: 3, name: "oppo" }
];
[Link]('/products', (req, res) => [Link](products));
[Link]('/products/:id', (req, res) => {
const product = [Link](p => [Link]() === [Link]);
product ? [Link](product) : [Link](404).json({ message: "Not found" });
});
[Link]('/addproducts', (req, res) => {
const { id, name } = [Link];
const newProduct = { id, name };
[Link](newProduct);
[Link](201).json(newProduct);
});
[Link]('/updateproducts/:id', (req, res) => {
const product = [Link](p => [Link]() === [Link]);
if (product) {
[Link](product, [Link]);
[Link](product);
} else [Link](404).json({ message: "Not found" });
});
[Link]('/deleteproducts/:id', (req, res) => {
const index = [Link](p => [Link]() === [Link]);
if (index !== -1) [Link]([Link](index, 1));
else [Link](404).json({ message: "Not found" });
});
[Link](3000, () => [Link]('Server running on [Link]
### Step 7: Update `[Link]`
Ensure your `[Link]` includes:
```json
{
"name": "expressjs",
"version": "1.0.0",
"type": "commonjs",
"main": "[Link]",
"scripts": {
"start": "node [Link]",
"server": "nodemon [Link]"
},
"dependencies": { "express": "^4.21.2" },
"devDependencies": { "nodemon": "^3.1.9" }
}
```
### Step 8: Run the Server
```sh
npm start
npm run server
```
### Step 9: Install Postman and Test APIs
1. Download Postman from: [Link]
2. Open Postman and use the following API endpoints:
| Method | URL | Description |
|--------|-------------------------------|------------------|
| **GET** | `[Link] | Get all products |
| **GET** | `[Link] | Get product by ID |
| **POST** | `[Link] | Add a new product |
| **PUT** | `[Link] | Update product |
| **DELETE** | `[Link] | Delete product |
### Example JSON for POST/PUT Requests:
```json
{
"id": 4,
"name": "Laptop"
}
```
### Step 10: Stop the Server
Press **Ctrl + C** in the terminal to stop the server.
Congratulations! You have successfully set up and run an [Link] project in VS Code.