Open Source Express Server for mocked services. Mock your services and test as you wish
-
download or clone from the https://github.com/kwar0715/mockableExpress
-
open the project
-
run
yarn install
ornpm install
-
run the project by using below commands
- npm
npm run-script start
or on custom portnpm run-script start --port 80
- yarn
yarn start
or on custom portyarn start 80
- docker command
- build
docker build . -t <name>
- run
docker run -p 80:80 <name>
(if run in detached mode use-d
)
- goto mocakble express url
Default Port is 3000
- when you run the server first, You should change the default password. type username as
user
and password as12345678
- reset password
- enjoy :-)
we use forever to run mockableExpress in detached mode.
- install
npm install forever -g
- start mockable express
#goto mockable express root
forever start index.js
- stop mockable express server
forever stop index.js
Mockable express is providing token based authentication for endpoints,
- click on the menu icon on right side and create token
- create new domain and path,
- in the path creation view, enable the Authorization check box
- Add authentication to your header.
- Keep token safe.
- You can pass your true data as url parameters and make the custom responses
As an example your url can be like below one
<host>/users/:userID
<host>/users/?userID=123456
{
"userID":"123456"
}
then your response can be customized by the url paramter
{
Id : {{userID}},
Name: "Name"
}
then Id can be changed according to the userID parameter
You can use your own database to save and retrive your mocked data temporarly.
{
#save("<your key >","<your value>")#
}
save with parameters,
you are passing a value as URL, Query, Body parameter. you can add you parameter as below,
url : /users/:id/:value
{
#save("{{id}}","{{value}}")#
}
{
value: #get("<your key >")#
}{
}
you are passing a value as URL, Query, Body parameter. you can retrieve your data as below,
url : /users/:id
{
value: #get("{{id}}")#
}
#del("<your key >")#
you are passing a value as URL, Query, Body parameter. you can retrieve your data as below,
url : /users/:id
#del("{{id}}")#
You can return difference response according to a condition.
#if("value1",<operator>,"value2"){
response
}endif
value1 and value2 are compaired by the operator what you are using, if the value1 and value2 are satisfied by the comparison, so only the below response will be returened.
Operator | Discription |
---|---|
= | equal to |
!= | not equal |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
Ex:- suppose you need to send A if userId equals to 10, unless you need to send B as the response
url : /users/:id
#if("{{id}}",=,"10"){
A
}endif
B
you can add json object in the response
#if("{{id}}",=,"10"){
{
value: "A"
}
}endif
{
value: "B"
}
You can generate your response from iterative manner. if you want to repeate something you can use this syntax.
#for("<Count>"){
<Repeatable part>
}endfor
suppose you need to pring "Hello" 10 times,
#for("10"){
Hello
}endfor
then your response will be
HELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLO
add to json response
{
"value": "#for("10"){
Hello
}endfor"
}
You can make the response according to the array of comma seperated response. your body, query parameter can be a comma seperated array or string,
body param:
{
"arr": [1,2,3,4,5]
}
or
{
"arr": "1,2,3,4,5"
}
query param:
<host>/path?arr=1,2,3,4,5
usage can be
#foreach("[1,2,3,4,5]","ele"){
{{ele}}
}endforeach
element iterative element and you can use any name.
result will be
[1,2,3,4,5]
you can use this in json object.
{
"value":#foreach("[1,2,3,4,5]","ele"){
{{ele}}
}endforeach
}
so the response can be,
{
"value": [
1,
2,
3,
4,
5
]
}
You can define a constants in the response body. you can only pass strings and digits for the constant.
!<variable name>=<value>!
!<variable name>
suppose you need to make a constant called data and value on data should be printed twise.
!data=hello!
!data
!data
You can comment on your reponse.
/*
comment string
*/
You can create endpoints programmatically from upload endpoint. so first you have to enable the upload functionality. goto ** Menu Bar -> Enable Upload**
url : <host>/admin/upload
method : post
headers : Content-Type : application/json
body :{
[
{
"domainName":"testDomain1",
"pathName":"testPath1",
"pathUrl":"/testPath1",
"pathDescription": "testPath1",
"pathMethod":"post",
"pathStatus": 200,
"body":"testPath1"
},
{
"domainName":"testDomain2",
"pathName":"testPath2",
"pathUrl":"/testPath2",
"pathDescription": "testPath1",
"pathMethod":"get",
"pathStatus": 200,
"body":"#if(\"{{id}}\",=,\"10\"){{\"value\": \"A\"}}endif{\"value\": \"B\"}"
},
{
"domainName":"testDomain3",
"pathName":"testPath3",
"pathUrl":"/testPath3",
"pathDescription": "testPath3",
"pathMethod":"get",
"pathStatus": 200,
"body": "{\"messsage\":\"hello\"}"
}
]
}
You can change your response by query parameter without do any if else syntax. this is enabled only for upload method.
[
{
"domainName": "testDomain1",
"pathName": "testPath1",
"pathUrl": "/testPath1",
"pathDescription": "testDiscripion",
"pathMethod": "GET",
"pathStatus": 200,
"query": {
"parameter":"status",
"value":"test1",
"body":{
"message":"hello"
}
},
"body": "#Query"
}
]
#Query segment will replaced by the filtered query.
"parameters": [ // one query parameter
{
"condition": "templete", // parameter condition
"value": "templete2" // parameter value that will pass from the above parameter to show
}
]
Templete 1 will received for testDomain1/testPath1?templete=templete1 Templete 2 will received for testDomain1/testPath1?templete=templete2
- Node JS : https://nodejs.org/en/
- Express Server : https://expressjs.com/
- EJS : https://ejs.co/
- JSON-DB : https://github.com/Belphemur/node-json-db
- Winston Logger : https://github.com/winstonjs/winston
- Bootstrap : https://getbootstrap.com/
- Jquery : https://jquery.com/
- Kolitha Warnakulasooriya
- Pravinda Perera
- Sameera Jayawardana
- Kasun Kodithuwakku