Caretaker Express · PostgreSQL · Cedar

Module 1 · Our first Express application

1.1 npm init and a first Express server

Turn the empty folder into a Node.js project, install a pinned Express version, start a server and read a raw HTTP response.

≈ 35 min 19 steps 17 lines of code Replay verified

Starting point

You have an empty Git repository:

Text
caretaker/└── .git/

Outcome

By the end of this lesson:

Text
caretaker/├── .gitignore├── .npmrc├── package.json├── package-lock.json├── node_modules/        (ignored by Git)└── src/    └── server.js

npm start runs a web server on port 3000. curl and your browser both get a reply from it, and you can read every line of the HTTP response.

The problem

A browser hides most of HTTP from you. To understand cookies, redirects and 403 responses later, you must be able to see the raw request and response. So we build the smallest possible server and inspect it with curl, a command-line HTTP client that ships with macOS.

Build it

1. Create package.json

package.json describes the project: its name, its dependencies and its scripts. npm init -y writes one with default answers:

Run and comparecaretaker/
npm init -y
Wrote to /Users/you/code/caretaker/package.json:

{
  "name": "caretaker",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Three defaults are not what we want. Change them with npm pkg, which edits package.json for you so you do not break the JSON by hand:

Runcaretaker/
npm pkg set type=module
npm pkg delete main
npm pkg set scripts.start="node src/server.js"
  • "type": "module" makes every .js file an ES module. We use import and export, not require.
  • main matters only for packages that other code imports. Our app is not a library.
  • scripts.start is the command that npm start runs.

2. Pin exact versions

By default npm records a version range such as ^5.3.0, which allows newer minor versions on a fresh install. For a course, and for most applications, we want every machine to install the same versions. Create .npmrc in the project folder:

Create file.npmrcINI
save-exact=true

npm also writes package-lock.json, which records the exact version of every package in the dependency tree. We commit both files.

3. Install Express

Run and comparecaretaker/
npm install express@5.3.0

added 65 packages, and audited 66 packages in 5s

31 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Express is the web framework. It gives us a router (match a URL and method to a function), helpers to read requests and write responses, and a way to chain middleware. We install a specific version so your output matches the course.

Look at what changed in package.json:

Run and comparecaretaker/
cat package.json
{
  "name": "caretaker",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node src/server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "module",
  "dependencies": {
    "express": "5.3.0"
  }
}

4. Ignore what should not be committed

node_modules is generated by npm install from the lockfile, so it does not belong in Git. .env files will hold secrets later. Create .gitignore:

Create file.gitignoreText
node_modules/.env

5. Write the server

Create the folder src and the file src/server.js:

Create filesrc/server.jsJavaScript
import express from 'express'; const app = express();const port = 3000; app.get('/', (req, res) => {  res.send('Caretaker is running');}); app.listen(port, () => {  console.log(`Caretaker listening on http://localhost:${port}`);});

Line by line:

  • import express from 'express' loads the package we installed. Node finds it in node_modules/express.
  • express() creates an application: an object that holds routes and middleware, and that can handle HTTP requests.
  • app.get('/', handler) registers a route: when a GET request arrives for the path /, Express calls handler.
  • The handler receives req (the incoming request) and res (the response we are building). res.send(...) writes a body, sets sensible headers, and ends the response.
  • app.listen(port, callback) opens a TCP port and starts accepting connections. The callback runs once the port is open.

Run and inspect

Start the server:

Server terminal (leave it running)caretaker/
npm start
> caretaker@1.0.0 start
> node src/server.js

Caretaker listening on http://localhost:3000

The terminal is now busy running the server. Open a second terminal in the same caretaker folder for the next commands.

Send a request with curl. The -i option includes the response status line and headers in the output:

Run and comparecaretaker/
curl -i http://localhost:3000/
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 20
ETag: W/"14-VWHqwfCUxJiNXtXBeaIyRtF2N6I"
Date: Sat, 28 Nov 2026 14:39:01 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Caretaker is running

Read it from the top:

  • HTTP/1.1 200 OK is the status line. 200 means success.
  • X-Powered-By: Express is added by Express. It advertises the framework, which is not useful to anyone but an attacker; we remove it in Module 10.
  • Content-Type: text/html; charset=utf-8: res.send with a string defaults to HTML.
  • Content-Length is the body size in bytes. ETag is a fingerprint of the body that browsers can use for caching.
  • After one empty line comes the body: Caretaker is running.

Now ask for a path we did not define:

Run and comparecaretaker/
curl -i http://localhost:3000/issues
HTTP/1.1 404 Not Found
X-Powered-By: Express
Content-Security-Policy: default-src 'none'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 145
Date: Sat, 28 Nov 2026 14:39:01 GMT
Connection: keep-alive
Keep-Alive: timeout=5

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /issues</pre>
</body>
</html>

Express answers 404 Not Found with a small HTML page that says Cannot GET /issues. No route matched, so Express used its built-in final handler.

Open http://localhost:3000 in your browser as well:

localhost:3000/
The same response in the browser.
The same response in the browser.

Experiment

Change the message in src/server.js while the server is still running:

Editsrc/server.jsJavaScript
Remove the lines marked − and add the lines marked +. Unmarked lines stay as they are; they show you where the change goes.
  res.send('Caretaker is running');  res.send('Caretaker is running. Stay safe!');
Show the whole src/server.js after this change
import express from 'express'; const app = express();const port = 3000; app.get('/', (req, res) => {  res.send('Caretaker is running. Stay safe!');}); app.listen(port, () => {  console.log(`Caretaker listening on http://localhost:${port}`);});

Predict: what does curl return now?

Run and comparecaretaker/
curl -s http://localhost:3000/
Caretaker is running
Reveal Why the old text?

Node.js loaded server.js once, when the process started. Changing the file on disk does not change the code that is already running. You must restart the process. In the next lesson we let Node do that automatically.

Stop the server with Ctrl+C in the first terminal, then start it again:

Ctrl+C Stop the server in the server terminal.
Server terminal (leave it running)caretaker/
npm start
> caretaker@1.0.0 start
> node src/server.js

Caretaker listening on http://localhost:3000
Run and comparecaretaker/
curl -s http://localhost:3000/
Caretaker is running. Stay safe!

Now the new code runs. Put the original message back so your file matches the next lessons:

Editsrc/server.jsJavaScript
Remove the lines marked − and add the lines marked +. Unmarked lines stay as they are; they show you where the change goes.
  res.send('Caretaker is running. Stay safe!');  res.send('Caretaker is running');
Show the whole src/server.js after this change
import express from 'express'; const app = express();const port = 3000; app.get('/', (req, res) => {  res.send('Caretaker is running');}); app.listen(port, () => {  console.log(`Caretaker listening on http://localhost:${port}`);});
Ctrl+C Stop the server in the server terminal.

Checkpoint

Commit your work. git add -A stages every new and changed file; .gitignore keeps node_modules out.

Run and comparecaretaker/
git add -A
git commit -m "Start an Express server"
[main (root-commit) 7f90b55] Start an Express server
 5 files changed, 898 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 .npmrc
 create mode 100644 package-lock.json
 create mode 100644 package.json
 create mode 100644 src/server.js
Symptom Error: listen EADDRINUSE: address already in use :::3000

Another process already uses port 3000, often a server you started in a different terminal. Stop it with Ctrl+C there, or find it with lsof -i :3000 and stop it.

Symptom SyntaxError: Cannot use import statement outside a module

package.json does not contain "type": "module". Run npm pkg set type=module again.

Symptom curl: (7) Failed to connect to localhost port 3000

The server is not running, or it crashed. Look at the first terminal for an error message.

Recap and next step

You created a Node.js project with pinned dependencies, wrote a five-line Express app, and read a raw HTTP response: status line, headers, empty line, body. You also saw that a running process does not notice changes to its files.

Next we add real routes for maintenance issues, read route parameters and query strings, and let Node restart the server automatically.