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.
Starting point
You have an empty Git repository:
caretaker/└── .git/Outcome
By the end of this lesson:
caretaker/├── .gitignore├── .npmrc├── package.json├── package-lock.json├── node_modules/ (ignored by Git)└── src/ └── server.jsnpm 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:
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:
npm pkg set type=module
npm pkg delete main
npm pkg set scripts.start="node src/server.js"
"type": "module"makes every.jsfile an ES module. We useimportandexport, notrequire.mainmatters only for packages that other code imports. Our app is not a library.scripts.startis the command thatnpm startruns.
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:
save-exact=truenpm also writes package-lock.json, which records the exact version of every package in the dependency tree. We commit both files.
3. Install Express
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:
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:
node_modules/.env5. Write the server
Create the folder src and the file src/server.js:
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 innode_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 aGETrequest arrives for the path/, Express callshandler.- The handler receives
req(the incoming request) andres(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:
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:
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 OKis the status line.200means success.X-Powered-By: Expressis 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.sendwith a string defaults to HTML.Content-Lengthis the body size in bytes.ETagis 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:
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:

Experiment
Change the message in src/server.js while the server is still running:
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}`);});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?
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:
npm start
> caretaker@1.0.0 start
> node src/server.js
Caretaker listening on http://localhost:3000
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:
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}`);});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}`);
});
Checkpoint
Commit your work. git add -A stages every new and changed file; .gitignore keeps node_modules out.
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.
Open the checkpoint files for lesson 1.1 (separate page; only files from this lesson and earlier)