Implementation of jsend with:
- All responses return json (including errors)
- Limit HTTP.status to
- 200 OK
- 400 BAD REQUEST (client fault)
- 500 INTERNAL SERVER ERROR (server fault)
- On top of 1 and 2, use jsend standard, but all 4 fields are always returned:
- status - [OK,FAIL,ERROR],
- data - the data or the stack
- message - shortest message possible
- code - [200,400,500]
With SSH:
git clone git@github.com:rkristelijn/jsend.git
cd jsend && npm i
With HTTP:
git clone https://github.com/rkristelijn/jsend.git
cd jsend && npm i
purpose | command |
---|---|
start | npm start -s |
test | npm test -s (after server is running) |
{
"status": "SUCCESS",
"data": {
"hello": "world",
"api": "http://localhost:3000/api"
},
"message": "200: OK",
"code": 200
}
{
"status": "FAIL",
"message": "Route '/api' not found",
"data": [
"400: BAD REQUEST",
"Route '/api' not found",
{
"available": [
["http://localhost:3000/api/users", "http://localhost:3000/api/fail"]
]
}
],
"code": 400
}
{
"status": "SUCCESS",
"data": {
"users": [
{
"name": "admin",
"pass": "admin"
},
{
"name": "system",
"pass": "system"
},
{
"name": "henk",
"pass": "annie"
}
]
},
"message": "200: OK",
"code": 200
}
{
"status": "SUCCESS",
"data": {
"user": {
"name": "henk",
"pass": "annie"
}
},
"message": "200: OK",
"code": 200
}
I purposely created an error, calling i_will_crash_on_purpose()
that doesn't exist to see if a proper JSON is returned.
{
"status": "ERROR",
"message": "i_will_crash_on_purpose is not defined",
"data": [
"500: INTERNAL SERVER ERROR",
"ReferenceError: i_will_crash_on_purpose is not defined",
"at /home/gius/jsend/api/fail/fail-router.js:10:5"
],
"code": 500
}
Ignore the WARN per heavily discussed fsevent optional dependency for chokidar (via nodemon) in npm here on non-mac systems. There seems no way around it, like I did with the jQuery dependency here
Debug comes as a dependency of express. Debug can be tailored at the beginning of every file and filtered while starting the app.
you can use a .env file and dotenv or simply add the following to the start script in package.json:
"scripts": {
"start": "PORT=3000 DEBUG=jsend* nodemon ./bin/www"
To keep the test clean, I've written 3 simple validators for jsend that can be reused and keep the test themselves short, manageable and reusable.
The test might as well skip setting up the app in before(). This removes redundant code, makes the test simpler and test the actual app.
This is not best practice because of this scope is gone. Mocha strongly advices to avoid lambda's in test:
Passing arrow functions (aka "lambdas") to Mocha is discouraged. Lambdas lexically bind this and cannot access the Mocha context.