json-server-mock-api
is a demonstration project that provides a practical example of integrating authentication into a JSON Server mock API. This project not only illustrates how to secure mock API routes but also demonstrates best practices for setting up a mock server and generating dynamic, realistic data using Faker.
- Authentication Integration: Implement authentication in JSON Server to protect API routes.
- Best Practices for Mocking: Structured approach to setting up a mock API server.
- Data Generation with Faker: Use Faker to generate dynamic, realistic data for the mock API.
src
- db
-- db.json
-- index.js
-- seed-data.js
- middleware
-- authenticate.js
- routes
-- auth.js
- config.js
- server.js
Explanation:
-
/src
Directory: The main source code of the project.-
/db
: Contains files related to the mock database.db.json
: The mock database file, which JSON Server uses to create the API.index.js
: Central file for database configurations or exports.seed-data.js
: A script to generate and populatedb.json
with dynamic data using Faker.
-
/middleware
: Holds middleware functions.authenticate.js
: The authentication middleware to protect certain API routes.
-
/routes
: Contains files defining various API routes.auth.js
: Manages authentication-related routes, like login and token validation.
-
config.js
: A configuration file for the project, possibly including settings like database paths, API keys, or environment-specific configurations. -
server.js
: The main server file where JSON Server is configured and started. This file integrates all the routes, middleware, and database configurations.
-
Before starting, ensure you have the following installed:
- Node.js
- npm
- docker - if running with docker
To set up the project on your local machine:
-
Clone the repository:
git clone https://github.com/iamtalwinder/json-server-mock-api.git
-
Change directory to the project folder:
cd json-server-mock-api
-
Install the required npm packages:
npm install
The project uses Faker to generate mock data:
- Run the seeding script to generate
db.json
with fake data:npm run seed:data
- Running the Server
To start the mock API server:
npm start
The server will run on http://localhost:3000.
-
/login: For handling authentication requests.
-
/register: For creating user
-
Protected routes db.json:
- /users: Access user data.
- /posts: Retrieve posts data.
- /comments: Fetch comments data.
-
/protected: A custom route demonstrating protected data access.
-
Clone the repository:
git clone https://github.com/iamtalwinder/json-server-mock-api.git
-
Change directory to the project folder:
cd json-server-mock-api
-
Build the image
docker build -t json-server-mock-api .
-
Run project
docker run -p 3000:3000 json-server-mock-api
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Talwinder Singh - Email