Bytes
rocket

Your Success, Our Mission!

6000+ Careers Transformed.

Code Example (Node.js + Express)

Last Updated: 4th September, 2026

Code Example (Node.js + Express)

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

app.use(express.json());

// Sample database (in-memory)
let users = [
{ id: 1, name: "Taniya", age: 20 },
{ id: 2, name: "Rahul", age: 22 }
];

// PUT: Update user
app.put('/users/:id', (req, res) => {
 const userId = parseInt(req.params.id);
 const { name, age } = req.body;

 // Find user
 const user = users.find(u => u.id === userId);

 if (!user) {
  return res.status(404).json({
    error: "User not found"
  });
}

 // Replace data (full update)
user.name = name;
user.age = age;

res.status(200).json({
  message: "User updated successfully",
  data: user
});
});

// Server
app.listen(3000, () => {
 console.log("Server running on port 3000");
});

Technical Example

Request

PUT /users/2
Content-Type: application/json

{
 "name""Rahul Sharma",
 "age"23
}

Response

200 OK
{
 "message""User updated successfully",
 "data": {
  "id"2,
  "name""Rahul Sharma",
  "age"23
}
}

Important Points

1. Always Use express.json()

To parse incoming JSON data.

2. Validate Request Body

if (!name || !age) {
return res.status(400).send("Invalid data");
}

3. Handle Errors Properly

  • 404 → Resource not found

  • 400 → Bad request

  • 500 → Server error

4. PUT is Idempotent

Calling the same request multiple times → same result

Technical Example (Advanced)

With Validation Middleware

function validateUser(req, res, next) {
 const { name, age } = req.body;

 if (!name || !age) {
  return res.status(400).json({
    error: "Name and age are required"
  });
}

next();
}

app.put('/users/:id', validateUser, (req, res) => {
 const id = parseInt(req.params.id);
 const user = users.find(u => u.id === id);

 if (!user) {
  return res.status(404).send("User not found");
}

user.name = req.body.name;
user.age = req.body.age;

res.send(user);
});

 
Module 2: Implementing PUT in Node.jsCode Example (Node.js + Express)

Top Tutorials

Logo

Top 10 Machine Learning Projects with Source Code (Beginner to Advanced)

Explore the Top 10 Machine Learning projects with source code, from beginner to advanced. Learn real-world ML applications, build portfolio-ready projects, and master hands-on skills with step-by-step tutorials from AlmaBetter.

3 Modules10 Lessons1151 Learners
Start Learning
Logo
Careers in Tech

Technologies to Learn in 2026: Building the Future of Innovation

Explore the top technologies to learn in 2026 including Generative AI, Cloud, Cybersecurity, Web3, Data Science, AR/VR, Quantum, RPA, and Green Tech.

00 Lessons909 Learners
Start Learning
Logo
Careers in Tech

aws

This tutorial presents a structured, beginner-focused yet industry-aligned guide to Amazon Web Services, designed specifically for 2026 learning and career requirements

00 Lessons42 Learners
Start Learning
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebook
    instagram
    linkedin
    twitter
    youtube
    telegram

© 2026 AlmaBetter