Getting Started
This page guides you through installing Express, creating a minimal application, defining routes, and starting the server. It covers the essential steps to get a basic Express application running.
Prerequisites
Express requires Node.js version 18 or higher. You can verify your installed version with:
node -vIf this is a new project, create a package.json file first using the npm init command.
Installation
Install Express from the npm registry as a dependency of your project:
npm install expressThis command adds Express to your project's dependencies in package.json and installs the module into node_modules. Express requires no additional peer dependencies; all core functionality is included in the package.
Creating a Minimal Application
Create a file named app.js (or any name you prefer) and import the Express module. The following example creates an application instance, defines a single route, and starts a server.
import express from 'express'
const app = express()
app.get('/', (req, res) => {
res.send('Hello World')
})
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000')
})Step-by-step explanation
- Import Express: The
import express from 'express'statement imports the default export of the Express module, which is a function. - Create the app: Calling
express()returns an application object (app). This object is the central entry point for configuring routes, middleware, and server behavior. - Define a route: The
app.get(path, handler)method registers a route handler for HTTP GET requests at the specified path. In the example, a GET request to/triggers the callback, which sends the string'Hello World'as the response. - Start the server:
app.listen(port, callback)binds the application to a TCP port and begins accepting connections. The callback runs once the server is ready.
Defining Routes
Express provides methods for each HTTP verb. The most common ones are:
| Method | Signature | Purpose |
|---|---|---|
app.get(path, handler) | (path: string,...handlers: function[]) | Handle GET requests |
app.post(path, handler) | (path: string,...handlers: function[]) | Handle POST requests |
app.put(path, handler) | (path: string,...handlers: function[]) | Handle PUT requests |
app.delete(path, handler) | (path: string,...handlers: function[]) | Handle DELETE requests |
app.all(path, handler) | (path: string,...handlers: function[]) | Handle all HTTP methods |
The path parameter can be a string literal, a string pattern with parameters (e.g., /users/:id), or a regular expression. Route parameters are captured and exposed as req.params.
Using Middleware
Middleware functions have access to the request object (req), the response object (res), and the next function in the application's request-response cycle. They can execute code, modify the request and response objects, end the request-response cycle, or call the next middleware.
A simple logging middleware example:
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`)
next()
})The app.use() method mounts middleware at a specified path (defaulting to /). Middleware runs in the order it is registered. If a middleware does not call next(), the request is left hanging and subsequent middleware or route handlers will not execute.
Starting the Server
The app.listen() method is the standard way to start an Express server. It accepts the same arguments as Node's http.Server.listen():
app.listen(port, hostname, backlog, callback)Only the port argument is required. The callback is invoked when the server starts listening.
To run the application, execute the file with Node.js:
node app.jsYou should see the message Server is running on http://localhost:3000 in the console. Open a browser to http://localhost:3000 or use curl:
curl http://localhost:3000The response should be Hello World.
Verifying the Setup
After starting the server, verify that:
- The server process is running and listening on the configured port.
- A GET request to
/returns the expected response. - The console logs any middleware output (if configured).
Environment Variables
Express itself does not require any environment variables to run. However, the following environment variables are commonly used in Express applications (none are defined or required by the Express core library):
| Variable | Purpose | Default |
|---|---|---|
PORT | The port the server listens on | 3000 (or as passed to listen()) |
NODE_ENV | Environment mode (e.g., development, production) | (none) |
If you want to use PORT, modify the listen call:
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Server is running on port ${port}`)
})Next Steps
- Learn about the Architecture of Express, including the middleware stack and routing internals.
- Read the Overview for a high-level description of Express features.
- Explore the examples in the repository under the
examples/directory for patterns like content negotiation, error handling, and template engines.