Download and install Node.js.
Create a new project directory and navigate to it:
mkdir rest-api-nodejs
cd rest-api-nodejs
Initialize a Node.js project:
npm init -y
We’ll use Express to build the API and Nodemon for live reloading during development.
npm install express
npm install --save-dev nodemon
Update your package.json
to use Nodemon:
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
Create a file named index.js
:
touch index.js
Add the following code to set up your server:
const express = require("express");
const app = express();
app.use(express.json());
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
Run the server:
npm run dev
Here’s an example of basic CRUD operations for a users
resource:
const users = [];
// Get all users
app.get("/users", (req, res) => {
res.json(users);
});
// Create a new user
app.post("/users", (req, res) => {
const user = req.body;
users.push(user);
res.status(201).json(user);
});
// Update a user
app.put("/users/:id", (req, res) => {
const id = req.params.id;
const updatedUser = req.body;
users[id] = updatedUser;
res.json(updatedUser);
});
// Delete a user
app.delete("/users/:id", (req, res) => {
const id = req.params.id;
users.splice(id, 1);
res.status(204).send();
});
Use Postman or cURL to test the API endpoints:
Building a REST API with Node.js and Express is straightforward and scalable. This foundational knowledge will help you develop robust backend systems for modern web applications.
Ready to take it further? Explore database integration with MongoDB or authentication with JWT for a more advanced API.
Author’s Note: Hi, I’m Alloura Blueberry, a developer passionate about crafting efficient and scalable APIs. Connect with me on LinkedIn or check out my other articles on API development!
Join our newsletter and be the first to discover exclusive design insights, development tips, and updates on our latest blog posts.
We value your privacy and promise to only send meaningful content. No spam, ever.
and many more!
A talk is happening
Sharing My 2025 Projects
New Blog Post
Mastering Gradient Borders in CSS 🍭🧁
Uncover the fundamentals of popular JavaScript frameworks and how they empower designers and developers to build dynamic, interactive web experiences.
Discover the importance of wireframing in web design, its key tools, and tips for creating effective layouts that streamline the design process.
Make sure your website is accessible to everyone with these essential web accessibility best practices.