TExcelTAD
Home › Express & NestJS › Express Get Started

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

bash · try it yourself
mkdir notes-api && cd notes-api
npm init -y
npm install express
Wrote 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:

json
{
  "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:

js
// 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:

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:

bash · try it yourself
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:

node · try it yourself
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

text
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 with npm ci.

Test yourself

Which command restarts your server automatically when files change, without extra packages?

  1. node --reload server.js
  2. node --watch server.js
  3. npm restart --auto
  4. express dev

What does "type": "module" in package.json change?

  1. It installs Express as a module
  2. .js files are treated as ES modules, so you use import
  3. It enables TypeScript
  4. It marks the package as private

Which file should you commit to guarantee identical dependency versions everywhere?

  1. node_modules/
  2. package-lock.json
  3. .env
  4. npm-debug.log