NodeJS: Elegant API Response for your microservices

Kailash Yogeshwar
4 min readAug 8, 2020

--

Hey, folks its been a while after my previous blog. Let's discuss how we can create a generic API response handler for elegant response messages.

Setup Requirements

  • NodeJS Latest
  • A project with express installed
  • A basic User API
  • Postman for Testing API
  • VS Code Editor

Motive

I have been working mainly on the backend and have seen a lot of applications implementing RMH (Response Message Handler). Let's try to list some of the approaches used by developers

  • No external RMH: Here the response messages are written inside the controller file. It works but not at all recommended I also do it sometimes but avoid doing it beforehand.
  • ID-based Message Hander: Here a file generally response.json is used with some incremental code which I think doesn’t make sense is used by utility file which accepts the code. Not efficient as it is difficult to figure out the code of message that can be reused.

It is very crucial to think about RMH as the UI will rely on a specific message format like error key in response incase of API call failure and message key for a successful response.

Project Data

MockData

This are sample response messages. Let's assume we have huge applications and we should have dynamic messages like status or error in case of validation messages etc. You can implement using below code

This will work if you have a small application yet this can be cumbersome to maintain it.

Below approaches will help you to organize the response messages in a single file and use the same message with dynamic data. eg: invalid username XYZ.

Lets code our API Service

Assuming you have set up NodeJS

Create a Project
Make a directory UserServiceAPI and initialize the node project using npm init inside the created directory.

Install Dependencies

 npm install express body-parser

Create an entry point for your service
Create a file server.js at the root of your project.

touch server.js

Npm install Nodedemon as a dev dependency

 
npm install -D nodemon

Code

helpers/response.js

Explanation

Here we have created a response message helper with 3 API’s success, error, and validation.

exports.success: Success API will form the success response object.

Success Object Data Structure

{
message : string,
code: number,
error: boolean,
results: object|array
}

exports.error: Error API will form the error response object.

Error Object Data Structure

{ 
message: string,
error: boolean,
code: number
}

exports.validation: Validation handler for creating validation error response

{
code: number,
error: boolean,
errors: {object|array}
}

Server Endpoints

server.js

Responses

success
validation_response
error_response

Here we are creating some REST endpoint for our UserService

  • GET /users: List users
  • POST /user: Create a new user will return validation error if the name is not provided.
  • PUT /user/{id}: Update the existing user, validation error if id is not valid.
  • DELETE /user/{id}: Delete the user with id if the id is valid.

Conclusion

So that’s how we can create a more reliable and flexible API Service along with helper functions to send the elegant responses. There is still room for improvement on how to add dynamic meta to message. Let me know your thoughts.

--

--

No responses yet