Help me respond
This is a simple response helper which should make you life a bit easier. It supports localization and friendly messages for users.
Help-me-respond preconfigures HTTP responses for you by setting up the status code, processing the message and setting headers. You only need to call one of the API functions and pass the message in a form of a string or an object to it.
Prerequisites for usage
Your project is a nodejs server based on Express or something similar. Help-me-respond uses the res object from Express, which represents the HTTP response that an Express app sends when it gets an HTTP request.
Setup
Install with
npm i help-me-respond --save
Config setup
Create a config folder and put default.json in it. (more cool config setup at https://github.com/lorenwest/node-config).
You will need to add the following to your default.json file. File naming is up to you :) This is a basic setup for the i18n-nodejs - https://github.com/eslam-mahmoud/i18n-nodejs
{
"lang": "en",
"langFile": "../../config/locales.json"
}
langFile path is used in the library therefore, the path is relative to the index.js file from help-me-respond folder
- Create the specified above locales folder and locales file and add some messages there.
{
"NO_SHARING_WITH_YOURSELF": {
"en": "You cannot share the link with yourslef"
},
"NOT_OWNER": {
"en": "You are not the owner"
}
}
Some messages returned from the server are too technical for the user. So we would like to differentiate between those messages and user friendly messages. Simply add the following to your default.json configuration file. Now you can add message names in the friendlyMessages array.
{
"friendlyMessages": []
}
Once the message name is in the above array, the response will have a key friendlyMessage which makes it easy for front-end to differentiate between messages.
Configuration
Turn off data prefix
When you server returns any data, a prefix data is added to response.
You can turn it off by setting prefixNone to true in your config file. For more info about config see Config setup above.
Remove the default application/json header by setting disableJsonHeader to true in your config file. For more info about config see Config setup above.
API
res - Express res object
msg - string message, Error object, any other object
headers - array with http headers. If none provided the only header set by the library will be Content-type: application/json
returns HTTP response with 400 error code
If no message is specified will return message - BAD REQUEST
returns HTTP response with 404 error code
If no message is specified will return message - NOT FOUND
returns HTTP response with 403 error code
If no message is specified will return message - FORBIDDEN
returns HTTP response with 403 error code
If no message is specified will return message - UNAUTHENTICATED
response - an Express response object
returns HTTP response with 502 error code
This can be used when you are quering third party API. E.g. You will receive an error reponse for you request. You can call this function with the original response object. This way you will differentiate between you own server errors and third party API problems
returns HTTP response with 200 success code
returns HTTP response with 201 success code
general function to return any HTTP code you want
Response examples
Success messages
{
"friendlyMessage": "I am a friendly successfull message"
}
{
"message": "I am a very technical success message that users do not want to see"
}
Error messages
{
"code": 400,
"error": {
"friendlyMessage": "I am a friendly error message"
}
}
{
"code": 400,
"error": {
"message": "I am a very technical error message that users do not want to see"
}
}