All Guides
bootdev
freecodecamp
the-odin-project

How to Build a REST API with Node.js and Express (2026)

Build a working REST API from an empty folder to running routes. Every step explained, with the Express patterns you'll actually use on the job.

11 min read
2026-07-05

What you need before you start

You need Node.js installed and a code editor. That's it. No database, no framework beyond Express, no build tools. Check if Node is already on your machine: open a terminal and run node --version. If you see a version number like v20.x or higher, you're set. If not, download the LTS version from nodejs.org and install it. Node ships with npm, the package manager you'll use to pull in Express. One thing worth knowing up front: a REST API is just a program that listens for HTTP requests and sends back data, usually as JSON. When your phone app loads your messages, it's hitting a REST API. You're about to build a small version of the same thing.

Step 1: Set up the project

Make a folder and turn it into a Node project. In your terminal: mkdir my-api cd my-api npm init -y The npm init -y command creates a package.json file, which tracks your project's dependencies and settings. The -y flag accepts all the defaults so you don't have to answer questions. Now install Express: npm install express This pulls Express into a node_modules folder and records it in package.json. Express is the most widely used web framework for Node. It handles the low-level HTTP work so you can focus on your routes.

Step 2: Write the server

Create a file called server.js and add this: const express = require('express'); const app = express(); app.use(express.json()); const PORT = 3000; app.listen(PORT, () => console.log(`API running on port ${PORT}`)); Three things are happening. You import Express and create an app. You tell the app to parse incoming JSON with express.json(), which means request bodies show up as a normal JavaScript object on req.body. Then you start the server listening on port 3000. Run it with node server.js. You should see your log message. The server is up, but it doesn't answer any routes yet. That's next.

Step 3: Add your routes

A REST API maps HTTP methods to actions on a resource. For a list of tasks, the standard set looks like this: GET /tasks reads all tasks, POST /tasks creates one, PUT /tasks/:id updates one, DELETE /tasks/:id removes one. Here's a simple version using an in-memory array so you don't need a database yet: let tasks = [{ id: 1, title: 'Learn Express' }]; app.get('/tasks', (req, res) => res.json(tasks)); app.post('/tasks', (req, res) => { const task = { id: Date.now(), title: req.body.title }; tasks.push(task); res.status(201).json(task); }); app.delete('/tasks/:id', (req, res) => { tasks = tasks.filter(t => t.id !== Number(req.params.id)); res.status(204).end(); }); Notice req.params.id pulls the id out of the URL, and req.body.title reads the JSON you posted. The res.status() calls set the right HTTP status: 201 for created, 204 for deleted with no content.

Step 4: Test it

Restart the server (stop with Ctrl+C, run node server.js again) and hit your routes. The quickest way is curl from another terminal: curl http://localhost:3000/tasks curl -X POST http://localhost:3000/tasks -H "Content-Type: application/json" -d '{"title":"Write tests"}' The first command lists your tasks. The second creates one and returns it. If you prefer a graphical tool, Postman or the Thunder Client extension for VS Code do the same thing with a friendlier interface. When a POST returns your new task with an id, your API works. You now have all four CRUD operations answering real HTTP requests.

Step 5: Handle errors and next steps

A real API needs to fail gracefully. Add a check so a request for a missing task returns 404 instead of crashing: app.get('/tasks/:id', (req, res) => { const task = tasks.find(t => t.id === Number(req.params.id)); if (!task) return res.status(404).json({ error: 'Task not found' }); res.json(task); }); From here, the natural next steps are swapping the in-memory array for a real database (PostgreSQL with a library like Drizzle or Prisma is a common choice), adding input validation so bad data gets rejected early, and splitting routes into their own files as the project grows. If you want a guided path through backend work, our /learn/backend page pulls together free courses that cover exactly this progression, and /guides/how-to-become-a-backend-developer maps the wider skill set.

Boot.dev

Want structured backend practice?

Boot.dev teaches backend development through hands-on coding, not video. Their courses cover HTTP servers, databases, and API design with automated feedback on every exercise. A solid next step once you've built your first Express routes.

Explore Boot.dev

This is an affiliate link. We may earn a commission if you sign up, at no extra cost to you. We only recommend platforms we rate.

Frequently Asked Questions

Do I need to know JavaScript before building an Express API?

You need the basics: variables, functions, arrays, and objects. You don't need to be an expert. If you can read the code snippets in this guide and follow what they do, you're ready. If not, spend a couple of weeks on JavaScript fundamentals first, then come back.

What is the difference between Node.js and Express?

Node.js is the runtime that lets JavaScript run outside a browser, including on a server. Express is a framework that runs on top of Node and makes handling HTTP requests and routes much simpler. You need Node installed; Express is a package you add to make the work easier.

Do I need a database to build a REST API?

No, not to learn. You can store data in a plain array in memory, like this guide does, which is perfect for practice. The catch is that the data resets every time the server restarts. Once the routes make sense, adding a real database like PostgreSQL is the next step.

How do I test my API without a front end?

Use curl from the terminal or a tool like Postman or Thunder Client. They let you send GET, POST, PUT, and DELETE requests directly to your routes and see the responses, no front end required.

Recommended Courses

The Odin Project's Node.js path covers Express.js, databases with PostgreSQL, authentication, APIs, and deployment. Build real-world backend applications through hands-on projects.

120h
4.8
Details

Learn Node.js and Express by building real-world API projects. Covers npm, package.json, basic node, Express routing, and MongoDB. Free verified certificate.

300h
4.7
Details

Codecademy's interactive Git course covers the entire version control workflow: commits, branches, merging, rebasing, remotes, pull requests, and collaborating on GitHub.

8h
4.6
Details

More Guides