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

ExpressJS_Setup_Guide

React program

Uploaded by

bdvt blore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ExpressJS_Setup_Guide

React program

Uploaded by

bdvt blore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy