Example: GET /products asks for product data.
What are HTTP methods?
HTTP methods tell the server what the client wants to do with data. In Express, each method has a matching function such as app.get() or app.post(). fileciteturn1file0L4-L7
HTTP Methods at a glance
| Method | Simple meaning | Typical use | Express |
|---|---|---|---|
| GET | Read data | Retrieve information without creating, editing, or deleting it. | app.get() |
| POST | Create data | Send new data, usually to create a record. | app.post() |
| PUT | Replace all data | Replace an entire existing resource. | app.put() |
| PATCH | Update part of data | Change only selected fields. | app.patch() |
| DELETE | Remove data | Remove a resource. | app.delete() |
Example: registration sends a name, email and password to create a user.
Example: PUT /users/10 replaces the complete profile.
Example: change only a username with PATCH /users/10.
Example: a delete-account action could send DELETE /users/10.
Route examples
app.get("/users", (req, res) => {
res.send("List of users");
});
app.post("/users", (req, res) => {
res.send("New user created");
});
app.put("/users/:id", (req, res) => {
res.send(`User ${req.params.id} fully replaced`);
});
app.patch("/users/:id", (req, res) => {
res.send(`User ${req.params.id} partially updated`);
});
app.delete("/users/:id", (req, res) => {
res.send(`User ${req.params.id} deleted`);
});
One Express app for multiple methods
The source document creates index.js in a hello-world directory and defines GET, POST, PUT and DELETE routes on /. fileciteturn1file0L48-L66
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send("GET Request!");
});
app.post('/', function(req, res){
res.send("POST Request!");
});
app.put('/', function(req, res){
res.send("PUT Request!");
});
app.delete('/', function(req, res){
res.send("DELETE Request!");
});
app.listen(3000);
nodemon index.js. The source shows the server starting on port 3000. fileciteturn1file0L67-L76Source document, page 2: the browser displays the GET response at localhost:3000. fileciteturn1file0L74-L76
Testing with Postman
The source demonstrates testing POST and PUT requests in Postman using http://localhost:3000/, with the response body showing the corresponding message. fileciteturn1file0L81-L85
http://localhost:3000/Expected response: POST Request!
http://localhost:3000/Expected response: PUT Request!
http://localhost:3000/The source also shows a DELETE test.
HEAD and OPTIONS
HEAD
HEAD is like GET, but the server returns response headers rather than the actual response body. It can provide information such as content type, file size, or last-modified time. fileciteturn1file0L92-L103
app.head("/profile", (req, res) => {
res.set("Content-Type", "application/json");
res.set("Last-Modified", "Sun, 23 Aug 2026 10:00:00 GMT");
res.status(200).end();
});
OPTIONS
OPTIONS asks which communication options or HTTP methods are available for a URL. A common use is a browser's CORS preflight check before a cross-origin request. fileciteturn1file0L104-L115
app.options("/users", (req, res) => {
res.set("Allow", "GET, POST, OPTIONS");
res.status(204).end();
});
The source notes that cors middleware commonly handles browser CORS-related OPTIONS requests automatically in Express applications. fileciteturn1file0L113-L115
Why dynamic routes?
Static routes are fixed. Dynamic routes let you place parameters in the URL so the server can process different values. fileciteturn1file0L120-L125
Single parameter
var express = require('express');
var app = express();
app.get('/:id', function(req, res){
res.send('The id you specified is ' + req.params.id);
});
app.listen(3000);
Try http://localhost:3000/123. Replacing 123 with another value changes the response because the value is captured as req.params.id. fileciteturn1file0L133-L137
Source document, page 5: a browser request to a dynamic URL displays the supplied ID. fileciteturn1file0L133-L137
Multiple parameters in a URL
Express can capture more than one URL parameter. The source uses /things/:name/:id. fileciteturn1file0L139-L149
var express = require('express');
var app = express();
app.get('/things/:name/:id', function(req, res) {
res.send('id: ' + req.params.id + ' and name: ' +
req.params.name);
});
app.listen(3000);
http://localhost:3000/things/bob/12345bobbecomes
req.params.name12345becomes
req.params.idThe document emphasizes that the paths shown are different and do not overlap; if code should execute for /things itself, that path must be defined separately. fileciteturn1file0L154-L157
Restricting route parameters with a pattern
You can use a regular expression to restrict what a dynamic parameter is allowed to match. The source example requires the ID to be exactly five digits. fileciteturn1file0L158-L170
var express = require('express');
var app = express();
app.get('/things/:id([0-9]{5})', function(req, res){
res.send('id: ' + req.params.id);
});
app.listen(3000);
[0-9] means a digit, and {5} means five occurrences. So the route accepts a 5-digit ID./things/12345/things/1234/things/abcdeHandling a route that does not match
If none of the defined routes match a request, the source notes that Express can respond with a message such as Cannot GET <your-request-route>. It then demonstrates a final route that can return a custom invalid-URL message. fileciteturn1file0L169-L180
var express = require('express');
var app = express();
// Other routes here
app.get('*', function(req, res){
res.send('Sorry, this is an invalid URL.');
});
app.listen(3000);
Valid vs invalid example
http://localhost:3000/things/anits/12345The source shows a response containing the ID. fileciteturn1file0L191-L193
http://localhost:3000/things/id/12345The source shows the custom invalid-URL response. fileciteturn1file0L194-L196
Source document, page 7: browser screenshots illustrate a valid URL response and an invalid URL response.
Mind map: HTTP methods → routes → parameters
Read
Create
Replace / partially update
ROUTING
Remove
Metadata / available methods
:id, :nameTest yourself
What you should remember
- Express provides method-specific route functions such as
app.get()andapp.post(). fileciteturn1file0L4-L7 - GET reads, POST creates, PUT replaces, PATCH updates part, and DELETE removes.
- HEAD returns headers/metadata without the response body; OPTIONS can tell the client which methods/options are available. fileciteturn1file0L92-L115
- Dynamic routes use placeholders such as
:idand:name. Their values are available throughreq.params. fileciteturn1file0L125-L136 - Route patterns can restrict which parameter values are accepted. fileciteturn1file0L158-L172
- A catch-all invalid-URL route should come after the other routes. fileciteturn1file0L187-L189