Key Takeaways
- Start with a supported Node.js LTS release. As of October 17, 2026, Node.js 24 is the latest LTS line, and Express 5 is the current major line.
- Create a small project, install Express locally, and add an explicit npm start script.
- A single GET / route is enough to return Hello World to a browser or terminal client.
- This is a local learning server, not a production deployment template.
To create a local Express server, install Node.js, initialize a project with npm, install Express, and write a route handler that sends a response. This Node.js Express tutorial takes you through that complete setup, ending with a working Hello World page.
Node.js runs JavaScript outside the browser. Express adds routing and request/response helpers, so you can connect a URL to a JavaScript function without writing all the HTTP handling yourself.
Steps
- Install Node.js and verify your tools.
- Create a Node.js project.
- Install Express and write a GET route.
- Launch the app and test it locally.
1. Install Node.js
Open the official Node.js download page, select your operating system, and follow its installation instructions for the supported LTS release. LTS means long-term support; Current receives newer nonbreaking changes. Under the Node.js release policy, even-numbered release lines such as Node.js 26 enter LTS after their Current phase. Starting with Node.js 27, the project is moving to one major release per year, with every major line planned to enter LTS.
As of October 17, 2026, choose Node.js 24 LTS for this walkthrough rather than Node.js 26 Current. Node.js 20 is end-of-life and should no longer be your starting point. Express 5 requires Node.js 18 or higher, but that minimum requirement is not a recommendation to install an unsupported runtime.
After installation, open a terminal and check both Node.js and npm, the package manager used to install Express:
node --version
npm --versionFor the recommended setup, the Node.js version should begin with v24.; npm reports its own version separately. If either command isn't found, reopen your terminal after installation and check your installation method's instructions.
2. Create a Node.js Project
In your terminal, navigate to the directory where you keep projects, then run:
mkdir myproject
cd myproject
npm init -y
npm pkg set type=module "scripts.start=node index.js"npm init -y creates package.json without the interactive questionnaire. The next command adds two settings: type and scripts.start.
"type": "module"tells Node.js to interpret this project's JavaScript files as ES modules, allowing theimportsyntax used below."start": "node index.js", insidescripts, tells npm what to run when you enternpm start.
Open myproject in your editor, create a new file named index.js, and save it next to package.json. Using your editor avoids shell-specific file-creation commands.
You don't need to change the main field. It describes a package entry point; our start script explicitly selects index.js instead.
3. Install Express and Create a Route
From the project directory, install Express:
npm install expressExpress 5 is the current default major line installed by this command. With npm's default settings, installation adds Express to dependencies in package.json, installs packages into node_modules, and creates or updates package-lock.json.
Commit package.json and package-lock.json when you put the project in source control. The lockfile records the resolved dependency tree so later installations can reproduce it; don't commit node_modules. For more detail, see how npm install works.
Paste this complete server into index.js:
import express from 'express';
import process from 'node:process';
const app = express();
const port = 5000;
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(port, 'localhost', (error) => {
if (error) {
console.error('Could not start server:', error.message);
process.exitCode = 1;
return;
}
console.log(`Listening on http://localhost:${port}/`);
});The server deliberately listens on localhost for this local exercise. Its startup callback checks for errors before printing a success message; Express 5 passes server error events to that callback.
How the Express Hello World Example Works
express()creates the application object.app.get('/', handler)registers a function to handle GET requests to the root path,/.reqrepresents the incoming request, including information such as headers and query parameters. This route doesn't need to read it.resrepresents the response.res.send('Hello World')sends that string as the response body; Express treats a string response as HTML by default.app.listen()starts accepting connections on the specified host and port. Here, the port is5000.
Browser or curl
|
| GET /
v
Express route: app.get('/')
|
| res.send('Hello World')
v
Client receives: Hello World4. Launch and Test Your Express Server
Save the file, then run this command from myproject:
npm startnpm runs the configured node index.js command. You can also run node index.js directly; choose one launch command, not both at once.
After npm's script output, you should see:
Listening on http://localhost:5000/Keep that terminal open and visit http://localhost:5000/ in your browser. The page should display exactly Hello World, without an exclamation mark.
Test the Route From a Terminal
Alternatively, open a second terminal and use curl. The -i option includes the HTTP response headers alongside the response body.
curl -i http://localhost:5000/Look for a 200 OK response and Hello World in the body. On Windows, use curl.exe -i http://localhost:5000/ to invoke the curl executable rather than Windows PowerShell's curl alias.
Press Ctrl+C in the server terminal to stop it. After editing index.js, stop and restart the server before testing again; this start command doesn't enable automatic restarts.
Change the Port When 5000 Is Already in Use
An EADDRINUSE error means another server already occupies the address you're trying to use. Check for an earlier copy of your app still running in another terminal, or change const port = 5000; to const port = 5001;, restart, and visit http://localhost:5001/.
Optional: Configure the Port With an Environment Variable
To make the port configurable without repeatedly editing the source, replace the existing port declaration with this fragment:
// Replace the existing port declaration in index.js.
const port = Number(process.env.PORT || 5000);This reads PORT from the process environment, converts it to a number, and falls back to 5000 when the variable is unset or empty. Use a numeric value such as 5001.
For a cross-platform local example, create a file named .env in your project directory with:
PORT=5001Stop the running server, then launch this variant instead of npm start:
node --env-file=.env index.jsNode.js loads the file's variables into process.env. An existing environment variable takes precedence over the file, and the command fails if .env doesn't exist. Assuming PORT wasn't already set, test at http://localhost:5001/.
Common Mistakes
- Installing in the wrong directory: run the npm commands from
myproject, where yourpackage.jsonlives. Express belongs in this project's dependencies, not in a global installation. - Mixing module styles: this example uses ES modules. Keep
"type": "module"and the suppliedimportstatements together instead of mixing them with older CommonJS examples. - Testing a different path: this app defines only the root route. A request to an undefined path such as
/helloreturns404 Not Found.
Where Node.js and Express Fit
Node.js suits services that spend much of their time waiting for network or other asynchronous input/output. Its event loop coordinates callbacks while non-blocking operations complete, but CPU-heavy JavaScript can still block other requests in the same process. See the Node.js event loop explained for the underlying model.
Express keeps routing and middleware composition relatively small. Middleware is a function that participates in processing a request; once the root route works, continue with writing Express middleware.
Can You Use Something Other Than Express?
Yes. These alternatives differ in scope, not just syntax:
- Koa provides a small, async-function-based middleware core and doesn't bundle middleware into that core.
- Fastify provides schema-based request validation and response serialization.
- NestJS adds a higher-level application architecture above Express or Fastify; Express is its default HTTP framework.
Before You Deploy
Before exposing an application publicly, plan HTTPS, input validation, safe error responses, dependency maintenance, request logging, and shutdown behavior. Start with Express application security best practices and the official production guidance.
FAQ
How do I create and run a basic Express server?
Create a project with npm init -y, install Express, and write an index.js file containing an application, a route, and an app.listen() call. Add a start script that runs node index.js, then launch it with npm start.
Which Node.js version should I use with Express?
Use a supported LTS release; as of October 17, 2026, Node.js 24 is the latest LTS line. Express 5's minimum is Node.js 18, but meeting that minimum doesn't mean an old runtime is still supported.
How do I test an Express route locally?
Keep the server running and open http://localhost:5000/ in a browser, or run curl -i http://localhost:5000/ in another terminal. For this example, expect a 200 OK response with Hello World in the body.
Why does my Express app report that the port is already in use?
EADDRINUSE means another server already occupies the requested address. Stop the other server if it's yours, or select another port and update the URL you use for testing.
What is the difference between Express 4 and Express 5?
This tutorial's basic GET / route works with both, but Express 5 changes some route-matching syntax, forwards rejected promises from handlers to error middleware, and passes server errors to the app.listen() callback. Follow the migration guide rather than assuming every Express 4 example is interchangeable.
Further Reading
- Node.js downloads — installation options and the latest LTS release.
- Node.js release status — supported, Current, and end-of-life release lines.
- Express 5 API reference — applications, requests, responses, and routing.
- Express 5 migration guide — differences from Express 4.
- npm pkg documentation — editing package.json from the command line.
- npm lockfile documentation — dependency-tree recording and source control.
- Node.js command-line reference — execution options, including --env-file.
- Express production security guidance — precautions before deployment.
