Express.js app.put() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Application

Definition

In Express.js, app.put() registers handlers for HTTP PUT requests on a path. APIs often use it to replace a resource at a known URL, for example PUT /users/42 with a full JSON body.

It is one of Express’s app.METHOD() routing methods: only PUT matches. Pair it with express.json() so req.body is available, and read the id from req.params.

Syntax

Call app.put in your Express.js app. Start with this line.

Express
app.put(path, callback)
app.put(path, ...callbacks)
CallWhat it does
app.put(path, callback)Primary signature.
app.put(path, ...callbacks)Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

app.put(path, callback)
app.put(path, ...callbacks)

Parameters

ParameterDescription
pathRequired. Route path to match. Strings match the whole path. Arrays register the same handlers for each path.
callbackRequired. Middleware-style handlers (req, res, next). You may pass several, or an array of them.

Return Value

app.put() returns the same application object (app), so chaining is possible. In real apps you almost always ignore the return value and register each route on its own line.

Express
app.put('/items/:id', replace);
// => app

app.put('/items/:id', replace).get('/items/:id', show);

Express.js Examples

Example 1 — Basic PUT Route

Official-style homepage PUT — no body parsing needed.

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

app.put('/', (req, res) => {
  res.send('PUT request to homepage');
});

app.listen(3000);

How It Works

  1. Register PUT only. app.put('/', …) ignores GET and POST.
  2. Match the path. A PUT to / enters the handler.
  3. Send a body. res.send(…) finishes with status 200 by default.
  4. Other verbs need other routes. Add app.post('/') or app.get('/') as needed.

Example 2 — Replace by Id

Parse JSON, replace an existing user, return 404 if missing.

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

app.use(express.json());

const users = new Map([
  ['42', { id: '42', name: 'Ada' }]
]);

app.put('/users/:id', (req, res) => {
  const id = req.params.id;
  if (!users.has(id)) {
    return res.status(404).json({ error: 'Not found' });
  }
  const name = req.body && req.body.name;
  if (!name) {
    return res.status(400).json({ error: 'name is required' });
  }
  const user = { id, name: String(name) };
  users.set(id, user);
  res.json(user);
});

app.listen(3000);

How It Works

  1. Parse JSON. express.json() fills req.body.
  2. Capture the id. /users/:id puts the segment in req.params.id.
  3. Require existence. Missing keys return 404 (strict replace, not create).
  4. Overwrite. Store the full new object and return it with 200.

Example 3 — Upsert (Create If Missing)

PUT can also create at a client-chosen id — return 201 when new, 200 when replaced.

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

app.use(express.json());

const notes = new Map();

app.put('/notes/:id', (req, res) => {
  const id = req.params.id;
  const text = req.body && req.body.text;
  if (typeof text !== 'string') {
    return res.status(400).json({ error: 'text is required' });
  }
  const existed = notes.has(id);
  const note = { id, text };
  notes.set(id, note);
  res.status(existed ? 200 : 201).json(note);
});

app.listen(3000);

How It Works

  1. Client picks the id. The path :id is the resource key.
  2. Validate the body. Require a string text field.
  3. Check existence. Remember whether the key was already present.
  4. Status by outcome. 201 for first write, 200 for replace.

Runtime Support

app.put() is an Express.js application method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send the PUT request.

✓ Server · Express 4 & 5

app.put()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
app.put() Express on Node.js

Bottom line: Run examples with Node.js and the express package, then PUT with curl, Postman, or fetch.

Frequently Asked Questions

It registers one or more handlers for HTTP PUT requests on a path. Only PUT matches — GET or POST to the same path need their own routes.
Use POST to create when the server assigns the URL/id. Use PUT to replace a resource at a known URL (for example PUT /users/42).
Express does not parse JSON by default. Add express.json() (or express.urlencoded() for forms) before your routes so req.body is filled.
Common choices are 200 with the updated resource, or 204 with no body. Use 404 when the resource is missing (unless you intentionally upsert).
No. Express runs on a Node.js server. Browsers only send HTTP PUT requests (usually via fetch or XHR) to your API.

Next: app.render()

Render a view template to a string callback.

app.render() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful