A powerful and flexible mock API server that automatically generates realistic data based on your specifications using Faker.js, it also optionally integrates with a file-based CSV database "snap4db" to simulate persistent data and accordingly reacts as an API.
npm install snapsrv4u
const { MockApiServer } = require('snapsrv4u');
const server = new MockApiServer(4517); // default value
server.addRoute('/api/users', {
method: 'GET',
count: 10, // number of objects created
properties: { // description of all the objects (created with faker.js)
id: { type: 'id', zeros: 5 },
name: { type: 'name' },
email: { type: 'email' }
}
});
server.start(); // starting the server
Category | Types |
---|---|
Numbers |
number , float , id , uuid
|
Strings |
string , sentence , paragraph
|
Dates |
date , past , future , timestamp
|
Personal |
firstname , lastname , fullname , username , email , avatar , phone , gender , jobTitle , bio
|
Location |
latitude , longitude , city , country , address , zipcode
|
Internet |
url , ipv4 , password , useragent
|
Company |
company , department , catchPhrase
|
Commerce |
product , price , color
|
Boolean | boolean |
Custom |
faker with method and options (e.g., faker: { method: 'music.genre' } ) |
const server = new MockApiServer(port); // port defaults to 4517
// POST request
server.addRoute('/sign-up', {
method: 'POST', // required
collection: 'users', // required
properties: {
name: { type: 'name' }, // describes the the property
email: { type: 'email', unique: true } // can be a unique property
}
});
Gets data from body, params or query
// POST request
server.addRoute('/delete-from-known-collection/:id', {
method: 'DELETE',
collection: 'users'
});
server.addRoute('/delete-from-unknown-collection/:id', {
method: 'DELETE'
});
Gets data from body, params or query
// POST request
server.addRoute('/get-from-known-collection/:id', {
method: 'GET',
collection: 'users'
});
server.addRoute('/get-from-unknown-collection/:id', {
method: 'GET'
});
-
method
: HTTP method ('GET', 'POST' or 'DELETE') -
count
: Number of items to generate (1 for single object, >1 for array). will only work with quick start -
properties
: Object describing the data structure
Type | Description | Options |
---|---|---|
number |
Random number |
min , max
|
string |
Random string |
min , max (length) |
boolean |
Random true/false | - |
date |
Random date |
from , to
|
name |
Random full name | - |
email |
Random email address | - |
uuid |
Random UUID | - |
id |
Numeric ID with padding |
zeros (padding length) |
Or anything else from faker.js
Each property can have these configurations:
-
type
: (Required) Data type to generate -
min
: Minimum value/length -
max
: Maximum value/length -
zeros
: Number of digits for IDs -
from
: Start date for date ranges -
to
: End date for date ranges
- PUT requests
- Adding the option to send data automatically (on timer)
- Creating a log for incoming requests
- Customizable response (function)