0% found this document useful (0 votes)
0 views

ExpressJS_Setup_Guide

React program

Uploaded by

bdvt blore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

ExpressJS_Setup_Guide

React program

Uploaded by

bdvt blore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Express.

js Project Setup in VS Code

### Step 1: Install VS Code


1. Download and install VS Code from: https://code.visualstudio.com/download

### Step 2: Install Node.js


1. Download and install Node.js from: https://nodejs.org
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 Node.js Project


```sh
npm init -y
```

### Step 5: Install Dependencies


```sh
npm install express
npm install -D nodemon
```

### Step 6: Create `server.js`


1. In **VS Code**, create a file named **server.js** inside **expressjs**.
2. Copy and paste this code:
const express = require('express');
const app = express();
app.use(express.json());

const products = [
{ id: 1, name: "mi" },
{ id: 2, name: "iphone" },
{ id: 3, name: "oppo" }
];
app.get('/products', (req, res) => res.json(products));
app.get('/products/:id', (req, res) => {
const product = products.find(p => p.id.toString() === req.params.id);
product ? res.json(product) : res.status(404).json({ message: "Not found" });
});
app.post('/addproducts', (req, res) => {
const { id, name } = req.body;
const newProduct = { id, name };
products.push(newProduct);
res.status(201).json(newProduct);
});
app.put('/updateproducts/:id', (req, res) => {
const product = products.find(p => p.id.toString() === req.params.id);
if (product) {
Object.assign(product, req.body);
res.json(product);
} else res.status(404).json({ message: "Not found" });
});
app.delete('/deleteproducts/:id', (req, res) => {
const index = products.findIndex(p => p.id.toString() === req.params.id);
if (index !== -1) res.json(products.splice(index, 1));
else res.status(404).json({ message: "Not found" });
});
app.listen(3000, () => console.log('Server running on http://localhost:3000'));

### Step 7: Update `package.json`


Ensure your `package.json` includes:
```json
{
"name": "expressjs",
"version": "1.0.0",
"type": "commonjs",
"main": "server.js",
"scripts": {
"start": "node server.js",
"server": "nodemon server.js"
},
"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: https://www.postman.com/downloads/
2. Open Postman and use the following API endpoints:

| Method | URL | Description |


|--------|-------------------------------|------------------|
| **GET** | `http://localhost:3000/products` | Get all products |
| **GET** | `http://localhost:3000/products/2` | Get product by ID |
| **POST** | `http://localhost:3000/addproducts` | Add a new product |
| **PUT** | `http://localhost:3000/updateproducts/2` | Update product |
| **DELETE** | `http://localhost:3000/deleteproducts/1` | 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 Express.js project in VS Code.

You might also like