We use cookies to enhance your experience. By continuing to visit this site you agree to our use of cookies. Learn More
contact@techquestworld.com
+919547614783
Premium Article
Published 3 months ago

Build a REST API in 10 Minutes with Express.js (No BS Guide)

5 min read
427 views
Build a REST API in 10 Minutes with Express.js (No BS Guide)
Photo by Techquestworld

Learn how to build a complete REST API using Express.js in just 10 minutes. A no-fluff guide with real-world code and best practices.

🔹 Step 1: Initialize Your Project

mkdir express-api-demo && cd express-api-demo
npm init -y
npm install express

🔹 Step 2: Create Your Basic Server

// index.js
const express = require('express');
const app = express();
const PORT = 3000;

app.use(express.json());

app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});

🔹 Step 3: Define API Routes

let users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
];

// Get all users
app.get('/api/users', (req, res) => {
res.json(users);
});

// Get single user
app.get('/api/users/:id', (req, res) => {
const user = users.find(u => u.id == req.params.id);
user ? res.json(user) : res.status(404).send('User not found');
});

🔹 Step 4: Add POST and DELETE

// Add new user
app.post('/api/users', (req, res) => {
const user = {
id: users.length + 1,
name: req.body.name
};
users.push(user);
res.status(201).json(user);
});

// Delete user
app.delete('/api/users/:id', (req, res) => {
users = users.filter(u => u.id != req.params.id);
res.status(204).send();
});

🔹 Step 5: Basic Error Handling

app.use((req, res) => {
res.status(404).send('Route not found');
});

🔹 Bonus: Test With Postman or Curl

Use Postman or curl to test:

curl http://localhost:3000/api/users

🚀 Ready to build your next Express.js REST API? Bookmark this guide!

Author
TAPAS SAHOO

Developer by Profession, Techie by Heart

A curious mind with a love for writing and technology, dedicated to simplifying web development and programming topics while keeping up with the ever-changing tech landscape.

Discussion (0)

Replying to
Previous Article
15 Common Node.js Mistakes That Are Slowing D...

Node.js is powerful, but even a small mistake can significantly impact performance. Learn the 15 mos...

Next Article
Full Stack Backend Course Roadmap with Node.j...

A full beginner-to-advanced roadmap for backend developers using Node.js. Includes MySQL and MongoDB...

Related Articles

Laravel vs NodeJS in 2025: Which One Should You Learn First?
Laravel vs NodeJS in 2025: Which One Should You Le...

Laravel or NodeJS – which one should you learn in 2025? This guide helps you choose the right backen...

Node.js Async vs Await Explained with Real Examples
Node.js Async vs Await Explained with Real Example...

Master async and await in Node.js with this easy guide. Includes real code examples, pros/cons and p...

ExpressJS vs NestJS: Which Node.js Framework Should You Choose in 2025?
ExpressJS vs NestJS: Which Node.js Framework Shoul...

Discover the core differences between ExpressJS and NestJS. Whether you build APIs or full-scale app...

Build Your Own Cron Job Manager in Node.js | Beginner-Friendly Guide
Build Your Own Cron Job Manager in Node.js | Begin...

Learn how to create your own custom Cron Job Manager in Node.js using just one simple method. Automa...

Table of Contents