Express Get Started
Before writing routes you need a project folder, a package.json and the express package. This lesson sets up a project the way you would on your own machine and explains each file that appears.
Requirements
| Tool | Version | Check with |
|---|---|---|
| Node.js | 18 or newer (22/24 LTS recommended) | node --version |
| npm | comes with Node | npm --version |
| An editor | VS Code or similar | — |
Express 5 refuses to support Node versions older than 18, so upgrade first if node --version prints something like v16.x.
Create the project
mkdir notes-api && cd notes-api
npm init -y
npm install expressWrote to /home/you/notes-api/package.json:
{
"name": "notes-api",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
added 66 packages, and audited 67 packages in 2s
14 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm init -y writes a default package.json. npm install express downloads Express and its dependencies into node_modules/ and records them in package.json and package-lock.json.
Note: The exact package count and timing depend on the version npm resolves. That's normal.
What changed in package.json
After installing, package.json gains a dependencies entry. Add a "type" field and a couple of scripts by hand:
{
"name": "notes-api",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node src/server.js",
"dev": "node --watch src/server.js"
},
"dependencies": {
"express": "^5.1.0"
}
}| Field | Purpose |
|---|---|
"type": "module" |
.js files use ES modules (import/export). Omit it to use CommonJS (require) |
start |
how production starts the app: npm start |
dev |
node --watch restarts the server whenever a file changes — no extra tool needed |
^5.1.0 |
accept any 5.x version at or above 5.1.0, never 6.0 |
CommonJS or ES modules?
Both work with Express 5. Pick one style per project:
// CommonJS (no "type" field, or "type": "commonjs")
const express = require('express');
// ES modules ("type": "module", or a .mjs file)
import express from 'express';Most examples in this course use require because they are short scripts; the project lessons use ES modules.
The first server file
Create src/server.js:
import express from 'express';
const app = express();
const port = Number(process.env.PORT) || 3000;
app.get('/health', (req, res) => {
res.json({ status: 'ok' });
});
app.listen(port, (error) => {
if (error) throw error; // Express 5 passes listen errors (e.g. port in use) to the callback
console.log(`Listening on http://localhost:${port}`);
});Start it and try it from a second terminal:
npm run dev
# in another terminal:
curl http://localhost:3000/health> notes-api@1.0.0 dev
> node --watch src/server.js
Listening on http://localhost:3000
{"status":"ok"}
The same server, runnable
This version listens on a random port and calls itself, so it can be verified automatically:
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
res.json({ status: 'ok' });
});
const server = app.listen(0, async () => {
const res = await fetch(`http://localhost:${server.address().port}/health`);
console.log('status:', res.status);
console.log('content-type:', res.headers.get('content-type'));
console.log('body:', await res.text());
server.close();
});status: 200
content-type: application/json; charset=utf-8
body: {"status":"ok"}
Notice that res.json() set the Content-Type header for you, including the charset.
Useful files to add
notes-api/
├── node_modules/ installed packages (never commit)
├── src/
│ └── server.js
├── .gitignore node_modules, .env
├── package.json
└── package-lock.json exact versions (do commit)Tip: Commit
package-lock.json. It guarantees that your teammates, CI and production install exactly the same versions withnpm ci.
Test yourself
Which command restarts your server automatically when files change, without extra packages?
- node --reload server.js
- node --watch server.js
- npm restart --auto
- express dev
What does "type": "module" in package.json change?
- It installs Express as a module
.jsfiles are treated as ES modules, so you useimport- It enables TypeScript
- It marks the package as private
Which file should you commit to guarantee identical dependency versions everywhere?
- node_modules/
- package-lock.json
- .env
- npm-debug.log