Examples: Getting Started
The examples/ directory is a catalog of small Express applications. It includes focused examples for authentication, content negotiation, sessions, views, route parameters, resources, static files, and more.
These examples exist to show one behavior at a time, so the directory is a useful reading path before studying larger application structures. For the smallest starting point, read hello-world; for error middleware, read error; for parameters and resource operations, read params and resource.
Sources: examples/README.md:1-29
Core concepts
The examples directory
The examples directory is a list of runnable patterns, with each subdirectory named for the Express behavior it demonstrates.
Sources: examples/README.md:1-29
An Express application
An Express application is created by requiring the repository entry point and calling express().
Sources: examples/hello-world/index.js:3, examples/hello-world/index.js:5
Error-handling middleware
Error-handling middleware is a function whose first argument is the error, followed by the request, response, and continuation function.
Sources: examples/error/index.js:20-27
Route parameters
Route parameters are the request values used by the parameter-focused example's in-memory user data and resource handlers.
Sources: examples/params/index.js:7, examples/params/index.js:13-19, examples/resource/index.js:41-68
Resourceful routes
Resourceful routes group multiple HTTP operations around one resource; the resource example exposes user listing, lookup, deletion, and range formatting through the User handlers.
Sources: examples/README.md:18-19, examples/resource/index.js:30-37, examples/resource/index.js:41-68
How to choose a first example
Start with hello-world when you want the smallest application shape. Its visible setup requires the local Express package and creates an exported application by calling express(). The application can then be supplied as the callback to http.createServer, whose returned server can call listen.
var express = require('../../');
var app = module.exports = express()The important pattern is that the example exports the application function rather than hiding it behind another abstraction. That shape also lets the acceptance test import the example and send requests directly to it; the test checks that GET / returns Hello World and that GET /missing returns 404.
Use error next when you need to see how failures are turned into an HTTP response. The example imports Express, creates and exports an application, imports morgan as logger, and derives test from app.get('env'). Its error handler has the four-argument shape (err, req, res, next), logs the stack outside the test environment, sets status 500, and sends Internal Server Error.
This is the smallest direct demonstration of the error-handling signature: the error is first, and the normal request, response, and continuation arguments follow it. The separate error-pages example explains why the four-argument arity matters: Connect invokes error-handling middleware when it has an error, and only that kind of middleware remains in the error path.
The examples below make the directory’s progression explicit.
| Example | Focus | Why read it |
|---|---|---|
hello-world | A simple request handler | Learn the minimum application shape and verify a root response. |
error | Error middleware | See the (err, req, res, next) signature and a 500 response. |
params | Route parameters | Inspect an in-memory user collection alongside parameter-oriented setup. |
resource | Resource operations | Follow several operations over the same user collection. |
These entries are identified in the examples index as a simple request handler, error middleware, route parameters, and multiple HTTP operations on the same resource.
Sources: examples/hello-world/index.js:3, examples/hello-world/index.js:5, lib/application.js:577-596, test/acceptance/hello-world.js:2-19, examples/error/index.js:7, examples/error/index.js:8, examples/error/index.js:9, examples/error/index.js:10, examples/error/index.js:20-27, examples/error-pages/index.js:79-88, examples/README.md:11-19
How parameters and resources are demonstrated
Read params first if your immediate question is how route parameters fit into an example application. The example requires http-errors, requires the local Express package, creates an exported application, and defines a small users array containing named records. The shown evidence establishes the data and setup, while the directory index identifies params specifically as the route-parameters example.
Read resource when you want the broader route pattern: several HTTP operations applied to one user collection. It creates and exports an Express application and defines a users array with six named records. The User object then separates the operations into named handlers.
The handler names show the resource shape: index sends the complete collection, show selects by req.params.id and returns an error object when no user exists, destroy deletes by identifier and reports whether deletion occurred, and range slices a selected interval. The range handler branches on format: JSON sends the selected records, while HTML maps users to list items and joins them into one unordered list.
var User = {
index: function(req, res){
res.send(users);
},
show: function(req, res){
res.send(users[req.params.id] || { error: 'Cannot find user' });
},
destroy: function(req, res, id){
var destroyed = id in users;
delete users[id];
res.send(destroyed ? 'destroyed' : 'Cannot find user');
}
};Notice that the resource logic is kept in named functions rather than placed into one undifferentiated callback; each function sends its own result through res.
Sources: examples/params/index.js:7, examples/params/index.js:8, examples/params/index.js:9, examples/params/index.js:13-19, examples/README.md:18-19, examples/resource/index.js:7, examples/resource/index.js:9, examples/resource/index.js:30-37, examples/resource/index.js:41-68
How the example flow fits together
The following workflow shows the grounded progression from selecting an example to observing its behavior; it focuses on the visible initialization and response operations rather than assuming route declarations that are not included in the excerpts.
Evidence
- examples-indexexamples/README.md:1
- hello-appexamples/hello-world/index.js:3
- hello-appexamples/hello-world/index.js:5
- error-handlerexamples/error/index.js:20
- resource-handlersexamples/resource/index.js:41
The code-backed hand-offs are response operations: the error handler calls status and send, while the resource handlers call send, slice, map, and join. The diagram therefore describes the examples as focused demonstrations of application creation, error response, and resource response behavior—not as a complete substitute for the routing documentation.
Sources: examples/hello-world/index.js:3, examples/hello-world/index.js:5, examples/error/index.js:20-27, examples/resource/index.js:41-68
How it connects
The examples are the concrete starting points for the repository’s broader Express concepts. The application creation and server-start pattern connects to Application Lifecycle, which explains how an app initializes and boots an HTTP server.
The route-parameter and resource examples connect to Routing Architecture, which covers layered routers, route parameters, and path matching. Their handler registration and dispatch behavior connects to App Routing & Request Dispatch, which documents route registration and request handling.
The error example connects to Test Suite Structure and Acceptance Test Suite because the hello-world application is imported and exercised with real HTTP-style requests in acceptance coverage. It also connects to Response: Sending Data because the examples use res.send to produce response bodies.
Sources: examples/hello-world/index.js:5, lib/application.js:577-596, examples/README.md:18-19, examples/resource/index.js:41-68, test/acceptance/hello-world.js:2-19, examples/error/index.js:20-27
Key takeaways
hello-worldis the first example to read for the smallest visible Express application shape.- The error example demonstrates
(err, req, res, next)and returns status500withInternal Server Error. - Read
paramsfor route-parameter setup andresourcefor multiple operations over users. - Resource handlers use req.params.id and res.send to look up, delete, and format users.
- The examples directory is an index of focused Express patterns, not one single application.
Sources: examples/hello-world/index.js:3, examples/hello-world/index.js:5, examples/error/index.js:20-27, examples/README.md:18-19, examples/resource/index.js:41-68, examples/README.md:1-29