在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:micromata/http-fake-backend开源软件地址:https://github.com/micromata/http-fake-backend开源编程语言:JavaScript 95.7%开源软件介绍:http-fake-backend
It actually can serve the content of other file types as well as sending the files itself as response. Comes as a Node.js server. Useful for mocking, testing and developing independent of the »real« backend. ExampleLet’s say you need an endpoint like http://localhost:8081/api/example which should return:
It’s a matter of seconds to create this endpoint with help of this little hapi server. It might take a few seconds longer as setting up the well-made JSON Server but it’s way more flexible. Requirements
Installgit clone https://github.com/micromata/http-fake-backend.git
npm install Or with help of Yeoman npm install -g yo
npm install -g generator-http-fake-backend This comes in handy, because the Yeoman generator has a sub-generator to setup endpoints of your fake backend very convenient. See https://github.com/micromata/generator-http-fake-backend. Default AddressThe server runs at http://localhost:8081/ providing a page with links to all existing API endpoints. Start the serverThere are the following two options. During development
This way the server uses Later (eg. for tests in CI)
Just starts the server via node. Configure endpointsEach endpoint needs a configuration file in Example configurationsSimple Example
module.exports = SetupEndpoint({
name: 'simpleExample',
urls: [{
requests: [
{ response: '/response-files/simpleExample.json' }
]
}]
}); Advanced Example
module.exports = SetupEndpoint({
name: 'anotherExample',
urls: [{
params: '/read',
requests: [{
method: 'GET',
response: '/response-files/anotherExample.json'
}]
}, {
params: '/update/{id}',
requests: [{
method: ['PUT', 'PATCH'],
response: {
success: true
}
}, {
method: 'DELETE',
response: {
deleted: true
}
}]
}, ]
}); Serving different content types
module.exports = SetupEndpoint({
name: 'fileTypes',
urls: [{
params: '/json',
requests: [{
response: '/response-files/simpleExample.json'
}]
}, {
params: '/text',
requests: [{
response: '/response-files/example.txt',
mimeType: 'text/plain'
}]
}, {
params: '/html',
requests: [{
response: '/response-files/example.html',
mimeType: 'text/html'
}]
}, {
params: '/pdf',
requests: [{
response: '/response-files/example.pdf',
sendFile: true
}]
}]
}); Faking HTTP errors and status code
module.exports = SetupEndpoint({
name: 'statusCodes',
urls: [
{
params: '/boomError',
requests: [{
// Returns a 402 status code + error message provided by boom:
// {
// "error" : "Payment Required",
// "message" : "Payment Required",
// "statusCode" : 402
// }
statusCode: 402
}]
},
{
params: '/customError',
requests: [{
// Returns a HTTP status code 406 and a self defined response:
response: { error: true },
statusCode: 406
}]
},
{
params: '/regularResponse',
requests: [{
// Returns a 401 error provided by boom
// as defined on endpoint level
response: '/response-files/anotherExample.json'
}]
}
],
statusCode: 401
}); The configuration object in Detail:
Configure serverThe main config is handled via a file named # NODE_ENV
# Could be either `development` or `production`
NODE_ENV=development
# Port of the Server
SERVER_PORT=8081
# Port for running the tests
TEST_PORT=9090
# URL Prefix for the endpoints
# eg. http://localhost:8081/api/foo
API_PREFIX=/api
# Custom response header
#CUSTOM_HEADER_NAME=Authorization
#CUSTOM_HEADER_VALUE=Bearer eyJhbGciOiJIUzUxMiJ9
Related
LicensePlease be aware of the licenses of the components we use in this project. Everything else that has been developed by the contributions to this project is under MIT License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论