Skip to content

nick2726/secure_express_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Node.js Express License Security Status

πŸ” Secure Express API

A production-ready Express.js security template featuring HTTPS, JWT authentication, RBAC authorization, Helmet with custom CSP, rate limiting, and centralized error handling.

This project demonstrates how to implement layered API security using modern backend best practices.


πŸš€ Features

  • πŸ” HTTPS (TLS encryption)
  • πŸͺ– Helmet with custom Content Security Policy (CSP)
  • 🌍 CORS configuration
  • 🚦 Rate limiting (IP-based)
  • πŸ”‘ JWT Authentication
  • πŸ‘‘ Role-Based Access Control (RBAC)
  • 🧱 Global Error Handling Middleware
  • βš™ Modular project structure
  • πŸ“¦ Environment-based configuration

πŸ›  Tech Stack

  • Node.js
  • Express.js
  • Helmet
  • CORS
  • express-rate-limit
  • jsonwebtoken
  • bcryptjs
  • node-forge (for self-signed HTTPS)

πŸ“‚ Project Structure

secure-express-api
β”‚
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ app.js
β”‚   β”œβ”€β”€ config.js
β”‚   β”œβ”€β”€ middleware
β”‚   β”‚   β”œβ”€β”€ auth.js
β”‚   β”‚   └── error.middleware.js
β”‚   β”œβ”€β”€ routes
β”‚   β”‚   └── auth.routes.js
β”‚   └── utils
β”‚       └── asyncHandler.js
β”‚
β”œβ”€β”€ package.json
└── README.md

πŸ” Security Layers Implemented

1️⃣ HTTPS (TLS)

The server runs over HTTPS using a self-signed certificate for development.

⚠ Browsers will show a certificate warning in development. This is expected.


2️⃣ Helmet Security Headers

  • Content Security Policy (CSP)
  • X-Frame-Options
  • HSTS
  • X-Content-Type-Options
  • Referrer Policy

3️⃣ CORS Protection

Restricts allowed origins via environment configuration.


4️⃣ Rate Limiting

Limits excessive requests per IP to prevent abuse and brute-force attempts.


5️⃣ JWT Authentication

  • Secure login endpoint
  • Token expiration
  • Protected routes
  • Token verification middleware

6️⃣ Role-Based Access Control (RBAC)

  • Role embedded inside JWT
  • Admin-only route protection

7️⃣ Centralized Error Handling

  • Global error middleware
  • Async wrapper for promise-based routes
  • Clean JSON error responses

βš™ Installation

Clone the repository:

git clone https://github.com/nick2726/secure_express_api.git
cd secure_express_api

Install dependencies:

npm install

πŸ”§ Environment Variables

Create a .env file in the root:

PORT=3000
JWT_SECRET=your_super_secret_key
JWT_EXPIRES_IN=1h
CLIENT_URL=https://localhost:3000
RATE_LIMIT_WINDOW=15
RATE_LIMIT_MAX=100

β–Ά Running the Server

npm run dev

Server will run at:

https://localhost:3000

πŸ§ͺ API Endpoints

πŸ”“ Public Route

GET /

Response:

{
  "success": true,
  "message": "Secure Express Server Running πŸš€"
}

πŸ”‘ Login

POST /api/auth/login

Body:

{
  "email": "admin@test.com",
  "password": "password123"
}

Returns:

{
  "success": true,
  "token": "JWT_TOKEN"
}

πŸ” Protected Route

GET /api/protected

Header:

Authorization: Bearer <token>

πŸ‘‘ Admin Route

GET /api/admin

Requires:

  • Valid JWT
  • role = admin

πŸ“Œ Future Enhancements

  • Request validation (Zod / Joi)
  • Structured logging (Pino)
  • Refresh token system
  • Per-user rate limiting
  • CSRF protection
  • Production SSL via reverse proxy

πŸ— Architecture Philosophy

This project follows:

  • Layered security approach
  • Middleware-driven architecture
  • Separation of concerns
  • Environment-based configuration
  • Production-style error handling

πŸ“œ License

MIT License


🀝 Contributing

Pull requests are welcome.

If you’d like to enhance the security layer or improve architecture, feel free to fork and submit a PR.


⭐ If You Found This Useful

Give the repository a star!


πŸ— Architecture Overview

Client ↓ HTTPS (TLS) ↓ Express App β”œβ”€β”€ Helmet (CSP, Security Headers) β”œβ”€β”€ CORS β”œβ”€β”€ Rate Limiter β”œβ”€β”€ Routes β”‚ β”œβ”€β”€ Auth Routes β”‚ β”œβ”€β”€ Protected Routes β”‚ └── Admin Routes (RBAC) └── Global Error Handler

About

Production-ready Express.js security template with HTTPS, JWT authentication, RBAC authorization, Helmet CSP, and rate limiting.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors