The main purpose of this tool is to emulate in the most detailed way the response of a json api, writing only a few lines of code, but maintaining great flexibility to define responses
Using npm
$ npm i -g auto-server-js
The library reads the files that its name begins with route and its extension is .js
Examples:
- route-users.js
- routeAuth.js
- route.js
Default of all properties you can define
module.exports = [
{
method: "get", // can be: 'post' | 'put' | 'delete'
path: "/authenticate",
routeType: 'normal', // can be: 'pagination' | 'file'
showBody: false,
showHeaders: false,
showParams: false,
showQuery: false,
showReponse: false,
responseStatus: 200,
responseTime: 0,
responseHeaders: {
'Content-Type': 'application/json'
},
defaultResponseSize: 20, // define how many items sends when routeType is pagination
paginationNames: {
page: 'n_pages', // change default paginate query names for request
limit: 'page_limit'
},
itemsPath: 'elements', // path in response object to insert pagination elements
response: {
success: true,
name: 'John',
user_id: 2
}, // if routeType is 'file' this field must be: './path/to/file/hello.txt'
},
];
Use single file
$ asjs --port 4000 --file ./routes-users.js
Use files in the current directory and port 4000
$ asjs
GET http://localhost:4000/hello
module.exports = [
{
path: '/hello',
response: {
// optional response object
name: 'atenea',
surname: 'pinky',
age: 30
}
}
];
POST http://localhost:4000/hello
module.exports = [
{
path: '/hello',
method: 'post',
response: {
// optional response object
success: true
}
}
];
POST http://localhost:4000/slow_route
module.exports = [
{
path: '/slow_route',
method: 'post',
response: {
// optional response object
success: true
},
// wait 2 seconds and respond
responseTime: 2
}
];
GET http://localhost:4000/images/doge.jpeg
module.exports = [
{
path: '/images/doge.jpeg',
routeType: 'file',
response: './path/to/file/doge.jpeg',
responseContentType: 'image/jpeg',
responseHeaders: {
'Content-Disposition': 'attachment;filename=doge.jpeg;'
}
}
];
Request users with pagination query like this:
http://localhost:4000/users?page=1&limit=10
module.exports = [
{
path: '/users',
routeType: 'pagination',
itemsPath: 'elements', // path in response object to insert items
response: {
total: 100,
elements: [
{
// take the first object as example
id: 1,
username: 'atenea',
email: 'atenea@mail.com',
}
]
},
}
];
Default pagination query names are: 'page' and 'limit'
module.exports = [
{
path: '/users',
routeType: 'pagination',
itemsPath: 'elements', // path in response object to insert items
paginationNames: {
page: 'p',
limit: 'size'
},
response: {
total: 100,
elements: [
{
// take the first object as example
id: 1,
username: 'atenea',
email: 'atenea@mail.com',
}
]
},
}
];
And you can use: GET http://localhost:4000/users?p=1&size=10
Only write path property like express library to use params
module.exports = [
{
path: '/users/:id',
showParams: true,
response: { success: true },
}
];
You can use flags to see content in the request
module.exports = [
{
path: '/example',
response: { success: true },
showParams: true, // show params in url specified in path: /example/:param
showQuery: true, // show query fields in url: https://localhost:4000/example?user_id=1&active=false
showBody: true, // show request body
showHeaders: true, // show request headers
showResponse: true, // show response when is send
}
];