Building Modular Express Applications with RESTful API Design

Setting Up the Entry Point: app.js

The foundation of an Express application begins with a well-structured entry file. Start by initializing the Express server:

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

Next, integrate routing modules with a consistent API version prefix:

const apiRoutes = require('./router');
app.use('/api/v1', apiRoutes);
const PORT = process.env.PORT || 3000;

Apply essential middleware for request parsing and cross-origin support:

const cors = require('cors');

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());

Finally, launch the server:

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

RESTful API Design Principles

RESTful APIs follow standardized conventions for request structure and response behavior.

Request Guidelines:

  • Use HTTP verbs semantically:
    • GET – Retrieve resources
    • POST – Create new resources
    • PUT – Replace entire resources
    • PATCH – Partially update resources
    • DELETE – Remove resources
  • Represent resources as plural nouns in URLs (e.g., /users, not /getAllUsers)

Response Standards:

  • Return appropriate HTTP status codes:
    • 2xx Success
      • 200 OK – Standadr success for GET, PUT, PATCH
      • 201 Created – After successful POST
      • 204 No Content – After successful DELETE
    • 4xx Client Errors
      • 400 Bad Request – Malformed request
      • 401 Unauthorized – Missing or invalid credentials
      • 403 Forbidden – Insufficient permissions
      • 404 Not Found – Resource doesn’t exist
      • 405 Method Not Allowed – Valid auth but disallowed method
    • 5xx Server Errors
      • 500 Internal Server Error – Unexpected server failure
      • 503 Service Unavailable – Temporary downtime

Note: 3xx redirection codes are generally avoided in pure API design.

Modular Routing Structure

Organize routes using a dedicated router directory.

router/index.js acts as the central route aggregator:

const express = require('express');
const router = express.Router();
const { requestLogger } = require('../middleware/logger');

router.use('/users', requestLogger, require('./users'));

module.exports = router;

router/users.js defines user-specific endpoints with layered middleware:

const express = require('express');
const router = express.Router();
const userController = require('../controller/user');
const { validateUserInput } = require('../middleware/validator/userValidator');
const { authenticateToken } = require('../middleware/auth');

// Public routes
router.post('/register', validateUserInput, userController.register);
router.post('/login', validateUserInput, userController.login);

// Protected routes
router.get('/', authenticateToken, userController.list);
router.delete('/:id', authenticateToken, userController.remove);

module.exports = router;

Tags: Express nodejs rest-api middleware api-design

Posted on Sat, 07 Nov 2026 16:12:35 +0000 by OldWolf