If you have been working in web development for a while you have probably heard that you should know how to build an API. And Node.js with Express is one of the most common ways to do it.
In this guide we are going to walk you through building a REST API from scratch, step by step. By the end you will have a working API and a clear understanding of how it all fits together.
What Is a REST API?
A REST API is a way for different parts of a software system to communicate with each other.
Your frontend sends a request to the API. Get me this data. Create this record. Update this item. The API processes the request and sends back a response.
Almost everything in modern web development works this way. Your React frontend talks to an API. Your mobile app talks to an API. Third-party services connect through APIs. Knowing how to build one is a genuinely important skill.
What You Need Before You Start
You need Node.js installed on your computer. Download it from nodejs.org if you do not have it. You also need a code editor like VS Code and Postman for testing your API endpoints.
That is all. Let us get started.
Step 1: Set Up Your Project
Create a new folder for your project and open it in your terminal.
This creates a package.json file that tracks your project dependencies. Now install Express.
Step 2: Create Your Server
Create a file called server.js and add the following code.
const express = require('express')
const app = express()
const PORT = 3000
app.use(express.json())
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})
Run it with node server.js and you should see the message in your terminal. Your server is running.
The express.json() line tells Express to automatically understand incoming JSON data. You need this whenever you accept data from a client.
Step 3: Add Your Routes
Routes are the endpoints your API makes available. Each route handles a specific type of request.
Restart your server and open Postman to test your endpoints.
GET /users returns all users. GET /users/1 returns the user with ID 1. POST /users creates a new user. PUT /users/1 updates the user with ID 1. DELETE /users/1 removes the user with ID 1.
Step 4: Add Error Handling
Right now if something breaks your server will crash or return a confusing response. Add this global error handler just before your app.listen line.
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).json({
message: 'Something went wrong'
})
})
Step 5: Organise Your Code
For a real project keeping everything in one file becomes messy quickly. Create a routes folder and move your route code into separate files. Then import them into server.js.
This keeps your code clean and easy to work with as your project grows.
What to Learn Next
This is a solid foundation. To build a production-ready API you will also want to add a real database using MongoDB or PostgreSQL, user authentication with JWT tokens, input validation using a library like Joi and environment variables to keep your credentials secure.
Keep Building
The best way to get better at API development is to build something real. Pick a project idea and build the backend for it. You will run into real problems and solving them is worth more than any tutorial.
Building something with Node.js and need a team that knows the stack? CodingBrackets builds backend systems for startups and growing businesses. Let us talk.

Comments (0)
No comments yet.