Home Express.js Application app.put() Express.js app.put() Method 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.
app.put(path, callback)
app.put(path, ...callbacks) Call What 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 const express = require("express");
const app = express();
app.put(path, callback)
app.put(path, ...callbacks) Parameters Parameter Description path Required. Route path to match. Strings match the whole path. Arrays register the same handlers for each path. callback Required. 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.
app.put('/items/:id', replace);
// => app
app.put('/items/:id', replace).get('/items/:id', show); Express.js Examples 1. Basic PUT 2. Replace by id 3. Upsert (create if missing) Example 1 — Basic PUT Route Official-style homepage PUT — no body parsing needed.
const express = require('express');
const app = express();
app.put('/', (req, res) => {
res.send('PUT request to homepage');
});
app.listen(3000); PUT /
→ 200 PUT request to homepage
POST /
→ (no match from app.put — needs app.post) How It Works Register PUT only. app.put('/', …) ignores GET and POST.Match the path. A PUT to / enters the handler.Send a body. res.send(…) finishes with status 200 by default.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.
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); PUT /users/42
{"name":"Ada Lovelace"}
→ 200 { "id": "42", "name": "Ada Lovelace" }
PUT /users/99
{"name":"Lin"}
→ 404 { "error": "Not found" } How It Works Parse JSON. express.json() fills req.body.Capture the id. /users/:id puts the segment in req.params.id.Require existence. Missing keys return 404 (strict replace, not create).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.
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); PUT /notes/n1
{"text":"hello"}
→ 201 { "id": "n1", "text": "hello" }
PUT /notes/n1
{"text":"hello again"}
→ 200 { "id": "n1", "text": "hello again" } How It Works Client picks the id. The path :id is the resource key.Validate the body. Require a string text field.Check existence. Remember whether the key was already present.Status by outcome. 201 for first write, 200 for replace.Compatibility
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 What does Express.js app.put() do? 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.
When should I use PUT instead of POST? 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).
Why is req.body undefined on PUT? Express does not parse JSON by default. Add express.json() (or express.urlencoded() for forms) before your routes so req.body is filled.
What status code should a successful PUT return? Common choices are 200 with the updated resource, or 204 with no body. Use 404 when the resource is missing (unless you intentionally upsert).
Can browsers run app.put()? No. Express runs on a Node.js server. Browsers only send HTTP PUT requests (usually via fetch or XHR) to your API.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion