Making a NodeJS Api
AcaiBerii

Hello! My name is Steve. I like to make a lot of projects on Replit. One of my more recent projects is an API that my friends and I are making for another one of my projects.

Now, I'll teach you exactly how I made this API.

Instead of just copying and pasting, this is a fun tutorial to follow along with.

It requires just HTTP!

If you want to know how to setup Node/NodeJS Replit webserver, follow this tutorial for more information.

Make sure you have an index.js to run, by the way!

So first of all, we need variables representing our different modules. These can be made in your index.js with:

This introduces our NPM modules to our project.

Next, we need a way to make our server. This can also be made with:

This allows us to handle requests and responses. It should be self explanatory which are requests and responses and what operates code, but to clarify:

  • req is the variable for requests that are incoming to your server from users.
  • res is the variable for responses that are going out from your server to users.
  • The function that handles your code is in between the two brackets.

Now, let's work on the brains of your API.
We need to know which requests are POST requests and which aren't. Let's work on it!

Okay, this separates POST from other HTTP request methods, such as GET or SOAP, etc.

What is "req.method"? This is a property of our req variable that tells which method the request is using. In browsers, this is usually GET.

Now, we need events that fire when we get data. Look no further than to adding this into your if (req.method === 'POST') loop.

Whenever we receive data, it will add it to the variable "body". Now, let's make an event that will fire when all data is received!

Although this event has no parameters, we can use the "body" variable to get the data from the request.

Now, I suggest using JSON to make requests and make responses. To parse our body variable into JSON, let's use this!

Now we can get parts of the JSON variable with:

That gets the "text-type" property of the JSON if the request includes this:

Now, we can see if the text-type property is JSON with:

Now, to make responses to that request, put this in your if (text-type === 'JSON') loop.

This is what the server sends back as a response to the request.

Do you want to add more parameters for the JSON request? Combine multiple "if" loops testing for different parts of the parsed JSON.

Want to test your API without writing a program to do it yourself? I do recommend using a website such as Reqbin to test POST requests.

A "valid" POST request for this API should look like:

Make sure to initialize your server with:

If you don't do this, your server won't be able to listen to connections.

This runs the server on the port 8080.

And if you want to keep your API alive, install the package "keep-alive-agent" and use the code below to keep it running.

If you want to make errors easier than constantly typing things in to make responses, just search up some status codes and use this!

http.STATUS_CODES[405] is error 405, or "method not allowed". Change "405" to another HTTP status code for a different response. This includes codes such as http.STATUS_CODES[404] which is error 404, or "not found".

Some common questions are:

  • What if I don't give a response back? -- Well, the request will time out and the user won't get their desired information.
  • Why not use Express? -- HTTP is very similar. You can still use Express if you want to!

Now, although this is very basic, you can expand way more. Thank you for looking at my tutorial, and I hope you'll look at some more of my discussions soon.

Goodbye!

You are viewing a single comment. View All
DynamicSquid

Nice tutorial!

AcaiBerii

@DynamicSquid Thank you!