mock_serv.js v1.1.0
mock_serv.js
mock_serv.js is a simple mock server module for testing and mocking API endpoints in your application. It helps you simulate API responses with data stored in a JSON file and provides the ability to add delays to responses for testing purposes.
Installation
To install mock_serv.js in your application, run:
npm install mock_serv.jsUsage
1. Importing and Starting the Mock Server
After installing mock_serv.js, you can start the mock server in your application by importing and configuring it. Here’s an example:
// Import the mock server
const mockServer = require('mock_serv.js');
// Initialize the server with optional configurations (like a custom delay)
mockServer.start({
delay: 2000, // Optional: Add a delay of 2000ms (2 seconds) for all API responses
port: 3001 // Optional: Define the port (default is 3001)
});2. API Endpoints
The mock server exposes several predefined API endpoints based on the data in config/endpoints.json. Here are the available endpoints:
User Endpoints
- GET
/api/users: Fetch a list of users. - GET
/api/users/:id: Fetch a user by ID. - POST
/api/users: Create a new user. - PUT
/api/users/:id: Update a user by ID. - DELETE
/api/users/:id: Delete a user by ID.
Post Endpoints
- GET
/api/posts: Fetch a list of posts. - GET
/api/posts/:id: Fetch a post by ID. - POST
/api/posts: Create a new post. - PUT
/api/posts/:id: Update a post by ID. - DELETE
/api/posts/:id: Delete a post by ID.
Product Endpoints
- GET
/api/products: Fetch a list of products. - GET
/api/products/:id: Fetch a product by ID. - POST
/api/products: Create a new product. - PUT
/api/products/:id: Update a product by ID. - DELETE
/api/products/:id: Delete a product by ID.
Authentication Endpoints
- POST
/api/auth/login: Login and receive a token. - POST
/api/auth/register: Register a new user. - GET
/api/auth/me: Get the current logged-in user’s details.
Error Handling Endpoints
- GET
/api/error: Simulate an internal server error (500). - GET
/api/not-found: Simulate a resource not found error (404). - GET
/api/slow: Simulate a slow response with a delay.
3. Optional Configuration
You can configure the server with the following options:
delay(in milliseconds): Add a delay to all responses (default:0).port(number): Define the port on which the server will run (default:3001).
Here’s an example of configuring the server to run on port 3002 with a 1-second delay:
mockServer.start({
delay: 1000,
port: 3002
});4. Environment Variables
You can also configure some options using environment variables:
DELAY: Set a global delay for all responses (e.g.,DELAY=1000for 1 second).PORT: Set a custom port for the server to run on (e.g.,PORT=3002).
5. Starting the Mock Server
Once you've set up the mock server, directly it would be used in main app.
This will start the server on the default port (3001), or a custom port if you have set one.
6. Testing the Mock Server
You can now make API requests to the mock server endpoints using any HTTP client, like Postman or fetch in the frontend. For example:
Using fetch to get the list of users:
fetch('http://localhost:3001/api/users')
.then(response => response.json())
.then(data => {
console.log('Users:', data);
})
.catch(error => console.error('Error:', error));Using Postman
You can also use Postman to test the endpoints:
GET http://localhost:3001/api/usersto fetch users.POST http://localhost:3001/api/usersto create a new user.- And more based on the endpoints listed above.
7. Stopping the Mock Server
To stop the mock server, simply press Ctrl + C in your terminal.
Conclusion
With mock_serv.js, you can easily mock API responses for testing and development without needing a real backend. It’s simple to configure, and you can add delays to simulate real network conditions.