Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backend for Devs

A high-performance mock API and file management system for rapid frontend development and prototyping.

🌟 Overview

Backend for Devs is a lightweight, configurable backend server built on top of JSON Server. It is designed to provide frontend engineers with a realistic development API that supports CRUD operations, static file serving, file uploads, and optional clustering for high‑load testing.

This project emphasizes simplicity, speed, and ease of use—everything you need to spin up a local API in seconds without writing a single line of backend code.


🎯 Key Features

  • Full REST API automatically generated from store/data/db.json.
  • File Upload Support via multipart/form-data using multer with checksum‑based renaming.
  • Static Asset Serving from /public endpoint.
  • Cluster Mode for multi‑core load simulation using Node's cluster module.
  • Middleware Inspection with request/response logging and body adaptation.
  • JWT‑based Authentication with login endpoint and route protection.
  • Simple Configuration—everything is handled through code conventions and a single JSON file.

🚀 Getting Started

Prerequisites

  • Node.js (LTS recommended, v18+)
  • npm or yarn

Installation

cd backdev
npm install

Development Mode

Start the server with automatic restarts using nodemon:

npm run dev

The API will be available on http://localhost:3000 (or the port specified by PORT).

Default response:

The Backend for Devs is listened at port 3000

Cluster Mode

To spawn one worker per CPU core and simulate a production‑like environment:

npm start

This runs src/cluster.js, which forks the main server into multiple instances.


🗂 Project Structure

backdev/
├── src/
│   ├── adapter.js        # request/response middleware
│   ├── auth.js           # JWT auth and login handler
│   ├── cluster.js        # cluster launcher
│   ├── fileUtils.js      # file I/O helpers
│   ├── main.js           # entrypoint for single‑process mode
│   ├── Server.js         # JSON Server configuration
│   ├── storage.js        # multer storage and static routes
│   └── Multer/DiskStorage.js
├── store/
│   ├── data/db.json      # mock database
│   └── public/           # uploaded files served statically
├── package.json
└── README.md

🔧 Configuration & Customization

  • Database: edit store/data/db.json; every top‑level key becomes a REST resource.
  • Static route: files stored under store/public/<resource> are served at /public.
  • Authentication: modify SECRET_KEY and payload structure in src/auth.js.
  • Upload behaviour: customize hashing, renaming, and cleanup in src/storage.js.
  • Middleware: adapt request/response logging, body coercion, or resource‑specific adapters in src/adapter.js.

📁 File Upload Flow

  1. Client POSTs/PUTs to a resource endpoint with Content-Type: multipart/form-data.
  2. multer intercepts and saves files to store/public/<resource>.
  3. After upload, files are renamed using a SHA256 hash.
  4. Middleware logs upload details and ensures the incoming JSON body fields are normalized.

Files can then be accessed via GET /public/<resource>/<filename>.


🔐 Authentication

  • Login: POST /login with JSON body { "email": "...", "password": "..." }.
  • JWT: response includes a token field (Bearer <token>).
  • Protected Routes: all following routes are guarded by AuthMiddleware once login is successful.

The middleware checks for the Authorization header and verifies the JWT using a hard‑coded secret.


📦 Dependencies

"dependencies": {
  "express": "^4.18.2",
  "express-interceptor": "^1.2.0",
  "json-server": "^0.17.1",
  "jsonwebtoken": "^8.5.1",
  "multer": "^1.4.5-lts.1"
},
"devDependencies": {
  "nodemon": "^2.0.20"
}

✅ Usage Examples

Create or update a user:

curl -X POST http://localhost:3000/users -H "Content-Type: application/json" -d '{"name":"Alice","email":"alice@mail.com"}'

Upload a file to products:

curl -F "file=@/path/to/image.jpg" http://localhost:3000/products

Login and use the token:

curl -X POST http://localhost:3000/login -d '{"email":"admin@mail.com","password":"admin123"}'
# copy token from response
curl http://localhost:3000/users -H "Authorization: Bearer <token>"

🧪 Testing & Validation

No automated tests are provided by default, but the endpoint behavior can be verified using the .insomnia collection included in the project. Import it into Insomnia or any other API client.


📂 Insomnia Workspace

The .insomnia folder contains a workspace with predefined requests, environments, and unit tests to exercise the API.


👨‍💻 Developer Notes

  • To reset the database, simply edit or replace store/data/db.json.
  • Uploaded files are permanent; manually clean store/public if necessary.
  • Cluster mode forks workers automatically on crash (except for graceful exits).

📜 License & Credits

MIT License. Developed by Luis Caldas.

Feel free to fork or adapt for your own projects—for devs, by devs!

About

Json server Backend to development with support the upload files

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages