Skip to content

This repository serves as the eBookStore-ReactWebsite's mock-server backend. eBookStore is a React E-Commerce Web Application where you can place an order for codebooks. Frontend (deployed on Netlify) and Backend (deployed on Render) are separately developed here. Filter, Search Option, JSON-Server, JSON-Server-Auth, JSON Web Tokens (JWT) are used.

Notifications You must be signed in to change notification settings

arnobt78/Mock-Server-eBookStore--ExpressJS-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

eBookStore Mock Server

Screenshot 2024-09-03 at 19 04 12


Project Summary

The eBookStore Mock Server is a backend mock API server built with Express, json-server, and json-server-auth to simulate the eBookStore application’s backend. It provides a fully functional REST API for products, featured products, orders, and users, complete with authentication and custom access rules. This project is ideal for frontend/backend developers and learners who wish to understand API mockups, authentication, and rapid prototyping.


Table of Contents

  1. Project Structure
  2. Features
  3. Technology Stack
  4. Installation
  5. How to Run
  6. API Endpoints
  7. Authentication & Access
  8. Example Data & Scripts
  9. Learning & Teaching Notes
  10. Conclusion

Project Structure

.
├── .gitignore
├── data/
│   ├── db.json
│   └── routes.json
├── index.js
├── package.json
  • .gitignore: Specifies files/folders to ignore in Git.
  • data/db.json: Stores mock data for products, featured products, orders, and users.
  • data/routes.json: Custom route rules for the API.
  • index.js: Main entry point; configures and starts the Express server, JSON server, and authentication.
  • package.json: Project metadata, dependencies, and scripts.

View the project files directly for more details:


Features

  • Full REST API for eBookStore entities (products, featured_products, orders, users)
  • Authentication & Authorization using json-server-auth (configurable access levels)
  • Custom Routing via routes.json
  • CORS enabled for easy frontend integration
  • Mock Data for learning, prototyping, and frontend development
  • Easy Extensibility for new resources or routes

Technology Stack

  • Node.js: Server runtime
  • Express: Core server framework
  • json-server: Rapid REST API mock server
  • json-server-auth: Simple authentication/authorization layer for json-server

Installation

Prerequisites

Steps

  1. Clone the Repository:

    git clone <repository-url>
    cd Mock-Server--eBookStore
  2. Install Dependencies:

    npm install

How to Run

Start the server with:

npm start

API Endpoints

All endpoints are prefixed with /api:

  • GET /api/products — Fetch all products
  • GET /api/featured_products — Fetch all featured products
  • GET /api/orders — Fetch all orders
  • GET /api/users — Fetch all users

Custom routes and access rules are defined in data/routes.json:

{
    "/products*": "/444/",
    "/featured_products*": "/444/",
    "/orders*": "/660/",
    "/users*": "/600/"
}

Authentication & Access

Authentication is handled via json-server-auth and rules are set in index.js:

const rules = auth.rewriter({
    products: 444,           // Read-only
    featured_products: 444,  // Read-only
    orders: 660,             // Read & Write
    users: 600               // Full access
});
  • 444 = Read-only access
  • 660 = Read and write access
  • 600 = Full access (CRUD)

Authentication Example:
To access protected routes, you must register/login and use the provided token.


Example Data & Scripts

Example Product (db.json)

{
  "products": [
    {
      "id": 10001,
      "name": "Basics To Advanced In React",
      "overview": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Error unde quisquam magni vel eligendi nam.",
      "long_description": "Lorem ipsum dolor sit amet consectetur, adipisicing elit. Soluta aut, vel ipsum maxime quam quia, quaerat tempore minus odio exercitationem illum et eos, quas ipsa aperiam magnam officiis libero expedita quo voluptas deleniti sit dolore? Praesentium tempora cumque facere consectetur quia, molestiae quam, accusamus eius corrupti laudantium aliquid! Tempore laudantium unde labore voluptates repellat, dignissimos aperiam ad ipsum laborum recusandae voluptatem non dolore. Reiciendis cum quo illum. Dolorem, molestiae corporis.",
      "price": 29,
      "poster": "https://images.unsplash.com/photo-1633356122544-f134324a6cee?ixlib=rb-1.2.1&auto=format&fit=crop&w=650&q=40",
      "image_local": "/assets/images/10001.avif",
      "rating": 5,
      "in_stock": true,
      "size": 5,
      "best_seller": true
    }
    // More products...
  ]
}

For more, see data/db.json


Learning & Teaching Notes

  • Purpose: This project is designed as a learning tool for developers to understand how to mock REST APIs, use authentication, and simulate a real backend for frontend development.
  • Modular Structure: Easily add more entities or extend existing data.
  • Security: Shows how to implement and test route protection in a mock server context.
  • Frontend Integration: Works perfectly with any frontend (React, Angular, Vue, etc.) — just use the /api endpoints.
  • Customization: Data, routes, and access rules can be easily edited for different scenarios.

Full Code Example: Server Setup (index.js)

import express from "express";
import jsonServer from "json-server";
import auth from "json-server-auth";

const server = express();
server.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', '*');
    next();
});

const router = jsonServer.router('./data/db.json');
server.use('/api', router);
server.db = router.db;

const middlewares = jsonServer.defaults();
const rules = auth.rewriter({
    products: 444,
    featured_products: 444,
    orders: 660,
    users: 600
});

server.use(rules);
server.use(auth);
server.use(middlewares);
server.use(router);

server.listen(8000);

Keywords

express, json-server, mock api, authentication, json-server-auth, REST, node.js, eBookStore, api prototyping, learning, teaching, backend, routes, middleware, mock data, CORS


Conclusion

The eBookStore Mock Server is a robust and easy-to-use mock backend for learning, teaching, and rapid prototyping. With real-world structure and authentication, it empowers developers to quickly test and demo frontend applications without building a full backend. Customize the data, routes, and rules as needed — and start building today!


About

This repository serves as the eBookStore-ReactWebsite's mock-server backend. eBookStore is a React E-Commerce Web Application where you can place an order for codebooks. Frontend (deployed on Netlify) and Backend (deployed on Render) are separately developed here. Filter, Search Option, JSON-Server, JSON-Server-Auth, JSON Web Tokens (JWT) are used.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published