Hackforge Academy

Category: React • Beginner

Published on 02 Mar 2026

Explanation

Create a Node.js project install the Express framework.

Code Example

npm init -y
npm install express

Explanation

Import Express, create an app instance and enable JSON middleware to parse incoming request bodies.

Code Example

const express = require('express');
const app = express();

app.use(express.json());

Explanation

Create a sample in-memory array to store user data.

Code Example

let users = [
  { id: 1, name: 'Praveen' },
  { id: 2, name: 'Kumar' }
];

Explanation

GET API to retrieve all users.

Code Example

app.get('/users', (req, res) => {
  res.json(users);
});

Explanation

GET API to retrieve a single user by ID. Returns 404 if not found.

Code Example

app.get('/users/:id', (req, res) => {
  const user = users.find(
u => u.id == req.params.id
);
  if (!user) return res.status(404).json(
{ message: 'User not found' }
);
  res.json(user);
});

Explanation

POST API to create a new user. Returns 201 status code after successful creation.

Code Example

app.post('/users', (req, res) => {
  const newUser = {
    id: users.length + 1,
    name: req.body.name
  };
  users.push(newUser);
  res.status(201).json(newUser);
});

Explanation

PUT API to update a user by ID.

Code Example

app.put('/users/:id', (req, res) => {
  const user = users.find(
u => u.id == req.params.id
);
  if (!user) return res.status(404).json(
{ message: 'User not found' }
);
  user.name = req.body.name;
  res.json(user);
});

Explanation

DELETE API to remove a user by ID.

Code Example

app.delete('/users/:id', (req, res) => {
  users = users.filter(
u => u.id != req.params.id
);
  res.json({ message: 'User deleted' });
});

Explanation

Start the Express server on port 3000.

Code Example

app.listen(3000, () => {
  console.log('Server running');
});

Want structured learning with real projects?

Join our Weekend Live Workshop and become job-ready faster.