How to build a REST API in Node.js

Building REST APIs in Node.js is fundamental for creating backend services that power modern web and mobile applications with standardized HTTP operations. With over 25 years of backend development experience and as the creator of CoreUI, I’ve built countless REST APIs for enterprise applications and open-source projects. The most effective approach is using Express.js with proper HTTP methods, status codes, and JSON responses following REST conventions. This provides a scalable, maintainable API architecture that integrates seamlessly with any frontend framework.

Use Express.js with proper HTTP methods and JSON responses to create a RESTful API following standard conventions.

const express = require('express')
const app = express()

// Middleware
app.use(express.json())

// In-memory data store (use database in production)
let users = [
  { id: 1, name: 'John Doe', email: '[email protected]' },
  { id: 2, name: 'Jane Smith', email: '[email protected]' }
]

// GET /api/users - Get all users
app.get('/api/users', (req, res) => {
  res.json({ success: true, data: users })
})

// GET /api/users/:id - Get user by ID
app.get('/api/users/:id', (req, res) => {
  const user = users.find(u => u.id === parseInt(req.params.id))
  if (!user) {
    return res.status(404).json({ success: false, message: 'User not found' })
  }
  res.json({ success: true, data: user })
})

// POST /api/users - Create new user
app.post('/api/users', (req, res) => {
  const { name, email } = req.body
  if (!name || !email) {
    return res.status(400).json({ success: false, message: 'Name and email required' })
  }

  const newUser = {
    id: users.length + 1,
    name,
    email
  }
  users.push(newUser)
  res.status(201).json({ success: true, data: newUser })
})

// PUT /api/users/:id - Update user
app.put('/api/users/:id', (req, res) => {
  const userIndex = users.findIndex(u => u.id === parseInt(req.params.id))
  if (userIndex === -1) {
    return res.status(404).json({ success: false, message: 'User not found' })
  }

  users[userIndex] = { ...users[userIndex], ...req.body }
  res.json({ success: true, data: users[userIndex] })
})

// DELETE /api/users/:id - Delete user
app.delete('/api/users/:id', (req, res) => {
  const userIndex = users.findIndex(u => u.id === parseInt(req.params.id))
  if (userIndex === -1) {
    return res.status(404).json({ success: false, message: 'User not found' })
  }

  users.splice(userIndex, 1)
  res.status(204).send()
})

app.listen(3000, () => {
  console.log('REST API server running on port 3000')
})

This REST API implements standard HTTP methods: GET for reading, POST for creating, PUT for updating, and DELETE for removing resources. Each endpoint returns proper HTTP status codes (200, 201, 404, etc.) and consistent JSON responses. The API follows RESTful conventions with resource-based URLs and appropriate request/response patterns.

Best Practice Note:

This is the foundation API pattern used in CoreUI’s backend templates for reliable, scalable services. Always include proper error handling, input validation, and consider adding authentication middleware for production APIs that handle sensitive data.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author