Express.js • Unit Notes

HTTP Methods & URL Binding

Learn how Express routes connect HTTP requests to server-side actions, and how dynamic URLs carry values into your application.

Student-friendly ELI10 explanations Visual learning Practical examples
1 • Big picture

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(). fileciteturn1file0L4-L7

Client
→
HTTP Method + URL
→
Express Route
→
Response
ELI10: Think of an HTTP method as an instruction on a delivery slip: GET means “show me,” POST means “here is new information,” PUT means “replace it,” PATCH means “change this part,” and DELETE means “remove it.”
2 • Core concepts

HTTP Methods at a glance

MethodSimple meaningTypical useExpress
GETRead dataRetrieve information without creating, editing, or deleting it.app.get()
POSTCreate dataSend new data, usually to create a record.app.post()
PUTReplace all dataReplace an entire existing resource.app.put()
PATCHUpdate part of dataChange only selected fields.app.patch()
DELETERemove dataRemove a resource.app.delete()
GET

Example: GET /products asks for product data.

POST

Example: registration sends a name, email and password to create a user.

PUT

Example: PUT /users/10 replaces the complete profile.

PATCH

Example: change only a username with PATCH /users/10.

DELETE

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`);
});
3 • Hands-on

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 /. fileciteturn1file0L48-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);
Run it: Save the file, then start the server with nodemon index.js. The source shows the server starting on port 3000. fileciteturn1file0L67-L76
Browser showing GET Request from the Express example.

Source document, page 2: the browser displays the GET response at localhost:3000. fileciteturn1file0L74-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. fileciteturn1file0L81-L85

POST
http://localhost:3000/
Expected response: POST Request!
PUT
http://localhost:3000/
Expected response: PUT Request!
DELETE
http://localhost:3000/
The source also shows a DELETE test.
4 • More HTTP methods

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. fileciteturn1file0L92-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();
});
Remember: “GET gives the content; HEAD gives information about the content.”

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. fileciteturn1file0L104-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. fileciteturn1file0L113-L115

5 • URL binding / building

Why dynamic routes?

Static routes are fixed. Dynamic routes let you place parameters in the URL so the server can process different values. fileciteturn1file0L120-L125

/things/
+
name
+
id
→
one dynamic route

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. fileciteturn1file0L133-L137

Browser showing a dynamic route response.

Source document, page 5: a browser request to a dynamic URL displays the supplied ID. fileciteturn1file0L133-L137

6 • Route parameters

Multiple parameters in a URL

Express can capture more than one URL parameter. The source uses /things/:name/:id. fileciteturn1file0L139-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);
Try it: http://localhost:3000/things/bob/12345
URL part
bob
becomes req.params.name
URL part
12345
becomes req.params.id

The 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. fileciteturn1file0L154-L157

7 • Pattern matching

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. fileciteturn1file0L158-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);
Pattern explained: [0-9] means a digit, and {5} means five occurrences. So the route accepts a 5-digit ID.
Matches
/things/12345
Does not match
/things/1234
Does not match
/things/abcde
8 • Invalid URLs

Handling 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. fileciteturn1file0L169-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);
Important: Put this catch-all route after all your other routes. Express matches routes from the start to the end of the file, including external routers that you require. fileciteturn1file0L187-L189

Valid vs invalid example

Valid
http://localhost:3000/things/anits/12345
The source shows a response containing the ID. fileciteturn1file0L191-L193
Incorrect
http://localhost:3000/things/id/12345
The source shows the custom invalid-URL response. fileciteturn1file0L194-L196
Source screenshot showing valid and invalid URL responses.

Source document, page 7: browser screenshots illustrate a valid URL response and an invalid URL response.

9 • Visual learning

Mind map: HTTP methods → routes → parameters

GET
Read
POST
Create
PUT / PATCH
Replace / partially update
EXPRESS
ROUTING
DELETE
Remove
HEAD / OPTIONS
Metadata / available methods
Dynamic URL
:id, :name
One-line memory trick: Method = what you want to do + URL = which resource you are talking about + route parameters = values carried inside the URL.
10 • Quick check

Test yourself

1. Which method is used to retrieve information?
2. Which method is intended to change only part of a resource?
3. In /things/:name/:id, where can the captured values be read?
4. Where should the catch-all invalid-URL route be placed?
Study summary

What you should remember

  • Express provides method-specific route functions such as app.get() and app.post(). fileciteturn1file0L4-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. fileciteturn1file0L92-L115
  • Dynamic routes use placeholders such as :id and :name. Their values are available through req.params. fileciteturn1file0L125-L136
  • Route patterns can restrict which parameter values are accepted. fileciteturn1file0L158-L172
  • A catch-all invalid-URL route should come after the other routes. fileciteturn1file0L187-L189