Complete "Lesson 1: REST and HTTP Fundamentals" first to unlock this lesson.Go to previous lesson

Introduction

Before you can build a REST API, you need a working project foundation. This lesson walks through every step: initializing a Node.js project, installing Express, writing a minimal server, and confirming it actually works. By the end you will have a running server that responds to HTTP requests — the base every subsequent lesson builds on.

This assumes Node.js (v18 or later) and npm are already installed on your machine. Run these two commands to verify before continuing:

bash
node --version
npm --version

If either command returns an error, download Node.js from nodejs.org (npm is bundled with it).

Initializing the Project

Create a new directory for your project and initialize it as a Node.js package. The init command generates a package.json file, which tracks your project metadata and dependencies.

bash
mkdir my-rest-api
cd my-rest-api
npm init -y

The -y flag accepts all defaults so you are not prompted for project name, version, and so on. You can edit package.json manually at any time. At this point your directory contains only that one file — no code yet.

Next, install Express:

bash
npm install express

This downloads Express and its dependencies into a node_modules folder and records the dependency in package.json under "dependencies". You will also see a package-lock.json created — this locks exact dependency versions so every developer on your team installs identical packages.

flowchart showing 3 steps: npm init creates package.json, npm install express adds Express to node_modules and updates package.json dependencies, then server.js imports Express to create the app

Building the First Server

Create a file called server.js in the project root. This file is your application entry point.

javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.json({ message: 'Server is running' });
});
app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});

Walking through each part: require('express') imports the framework. Calling express() creates an application instance stored in app. app.get() registers a route handler — the first argument is the URL path, the second is a callback that receives the request object (req) and response object (res). res.json() sends a JSON response and sets the Content-Type header automatically. Finally, app.listen() binds the server to a port and starts accepting connections.

Start the server:

bash
node server.js

You should see the console message. Open a browser to http://localhost:3000 or use curl:

bash
curl http://localhost:3000

Expected output:

json
{"message": "Server is running"}

Improving the Development Workflow

Restarting the server manually after every code change is tedious. Install nodemon as a development dependency — it watches your files and restarts automatically when it detects changes.

bash
npm install --save-dev nodemon

Then add a dev script to package.json:

json
{
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
}
}

Now run the server in development mode:

bash
npm run dev

Edit server.js, save, and nodemon restarts the process without you doing anything. The start script remains for production use where nodemon is not appropriate.

One more improvement: parse incoming JSON request bodies. Express does not do this by default. Add this middleware line before your routes:

javascript
app.use(express.json());

Without this, req.body will be undefined when a client sends a POST request with a JSON payload. Adding it now saves debugging confusion later.

Summary

You now have a fully functional Express project: a package.json tracking dependencies, Express installed, a server.js that listens on port 3000, a working GET route, nodemon for fast iteration, and JSON body parsing enabled. This is the exact skeleton used as the starting point for every REST API pattern covered in the rest of the course. The next lesson moves into routing — defining multiple endpoints and organizing them cleanly as the API grows.