BRBishant
  • Home
  • About
  • Skills
  • Experience
  • Education
  • Projects
  • Contact
  • Blog
  • Play
Resume
  • Home
  • About
  • Skills
  • Experience
  • Education
  • Projects
  • Contact
  • Blog
  • Play
  • Download Resume
← Back to Blog
Bishant RajbanshiMade with intention.
From Kathmandu, Nepal.
Back to top ↑
HomeRolesProjectsWriting
[email protected] ↗
Rajbanshi
© 2026 Bishant Rajbanshi
GitHub ↗LinkedIn ↗
Thanks for stopping by.
← Back to Blog
BackendApril 12, 20268 min read

Designing REST APIs with Express & MongoDB

Best practices for structuring a production-grade Node.js REST API with Express, Mongoose, JWT authentication, and proper error handling.

Designing REST APIs with Express & MongoDB
Node.jsExpressMongoDBREST API

A well-designed REST API is the backbone of any modern web application. Here's the architecture I've refined through projects like ArenaPro.

Project Structure

src/
  controllers/   # Route handlers
  models/        # Mongoose schemas
  routes/        # Express routers
  middleware/    # Auth, error handling
  utils/         # Helpers
  app.js
  server.js

Authentication with JWT

JSON Web Tokens are the standard for stateless authentication. Here's a minimal but secure setup:

// middleware/auth.js
const jwt = require('jsonwebtoken')

module.exports = (req, res, next) => {
  const token = req.headers.authorization?.split(' ')[1]
  if (!token) return res.status(401).json({ message: 'No token' })

  try {
    req.user = jwt.verify(token, process.env.JWT_SECRET)
    next()
  } catch {
    res.status(401).json({ message: 'Invalid token' })
  }
}

Centralised Error Handling

Never scatter try/catch blocks. Use a global error middleware and an async wrapper.

// utils/catchAsync.js
module.exports = fn => (req, res, next) => fn(req, res, next).catch(next)

// app.js — last middleware
app.use((err, req, res, next) => {
  const status = err.statusCode || 500
  res.status(status).json({ message: err.message })
})

Checklist for Production APIs

  • Validate all input with express-validator or Zod.
  • Rate-limit authentication routes.
  • Use HTTPS and set proper CORS headers.
  • Never return stack traces in production.
  • Index your MongoDB fields used in queries.
BR
Bishant Rajbanshi
Full Stack & ML Engineer
GitHubLinkedIn

More Articles

  • Building My First Agentic AI: A RAG Research Assistant, From Scratch8 min read
  • Why I Chose Computer Science — and What I've Learned5 min read
  • Intro to Machine Learning with Scikit-Learn7 min read
  • Building Full-Stack Apps with Next.js 146 min read
← PreviousWhy I Chose Computer Science — and What I've LearnedNext →Intro to Machine Learning with Scikit-Learn
← All Articles