Express app.put() Method

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 4 Code Examples
Express.js

What you’ll learn

  • How to define PUT routes using app.put(path, handler).
  • How to update existing resources using request body data.
  • How idempotent update semantics affect API behavior.
  • How to avoid common PUT route mistakes.

Overview

app.put() handles HTTP PUT requests and is commonly used to replace or fully update existing resources.

Update operations

Targets a known resource URL to change existing data.

Body-driven updates

Reads replacement/update fields from req.body.

Idempotent intent

Same request repeated should keep the same resulting state.

Syntax

javascript
app.put(path, callback)
app.put(path, middleware1, middleware2, ..., callback)
  • path: route path pattern for PUT requests.
  • callback/middleware: handler chain run on route match.
  • Use parser middleware so req.body contains incoming update data.
1

Basic PUT update route

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

app.use(express.json());

app.put('/users/:id', function (req, res) {
  res.json({ updated: true, id: req.params.id, data: req.body });
});
2

Validation for complete replacement

javascript
app.put('/profiles/:id', function (req, res) {
  var body = req.body || {};

  if (!body.name || !body.email) {
    return res.status(400).json({ error: 'name and email are required' });
  }

  var profile = {
    id: req.params.id,
    name: String(body.name).trim(),
    email: String(body.email).trim().toLowerCase()
  };

  res.status(200).json(profile);
});

📋 app.put() vs app.patch()

MethodTypical intentRequest style
app.put()Full replacement/updateSend full resource representation
app.patch()Partial updateSend only changed fields

🧪 Testing checklist

  • Verify update success path returns consistent response data.
  • Test missing required fields for validation failures.
  • Repeat same PUT request and verify final state remains consistent.
  • Confirm resource-not-found handling for invalid IDs.

Pitfalls to avoid

Missing parser

No update payload

Configure body parser middleware before PUT routes.

PUT/PATCH confusion

Inconsistent behavior

Define and document whether route expects full or partial payloads.

Weak validation

Corrupt resource state

Validate required fields before saving updates.

❓ FAQ

It defines a route handler for HTTP PUT requests on a specified path.
Use PUT when replacing or updating a resource at a known URL.
Sending the same PUT request multiple times should result in the same final resource state.
Use body parser middleware like express.json() and then access req.body.
Not exactly. PUT is commonly used for full replacement, while PATCH is for partial updates.

Summary

  • Core use: app.put() handles update routes for existing resources.
  • Inputs: read and validate update data from req.body.
  • Practice: keep semantics predictable and idempotent.
Did you know?

app.put(path, handler) is generally used for full updates and should behave idempotently in well-designed APIs.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

4 people found this page helpful