Hi friends, it’s been a while since write again in Medium. Let’s talk about API in NodeJS shall we?

The requirement for following this article

  1. Very basic JavaScript.
  2. Very basic NodeJS / ExpressJS.
  3. Very basic API.

Tools that you must have to follow this article

1. NodeJS

The important thing is **NodeJS **itself, you can download in here (https://nodejs.org/en). I recommending install the LTS version, I currently used v12.2.0_._

2. Postman

Postman is a powerful and friendly UI app for making an HTTP request, you can download in here (https://www.postman.com).

3. Text Editor

There’s a bunch Text editor out there, I prefer using **VSCode **which you can download here (https://code.visualstudio.com).

It’s fine if you want to use text editors like **Sublime Text **or Atom, just choose tools that match for you.

Why I made this article

You know what? I just tired of making response API with the same object over and over again, it’s just like repeating that’s you already done before, and that’s not good by the way, but I did it. What an ironic haha. But is a long time ago.

So, Let’s see the response API below, I have the users data which contains:

Image for post

Example response

Okay so, the data is fine. But, imagine you have a bigger app, and you must have a dynamic message that fire off when you hit a certain endpoint, maybe you need a status code or maybe you need validation or error object as well and the last maybe is, you want to configure your data object that sent to the client-side? Well, You imagine if you just code like this:

res.json({code: 200, someKey: "someData", data: { someData: "word" }})

That’s fine, but what if you have a lot of endpoints? that’s gonna be tired right? You write the same code over and over again.

This article comes in handy for handling those such things. What are you waiting for? Let’s scroll down!

#nodejs #api

NodeJS tutorial -  Make Your API Response Nicely
8.75 GEEK