Learn to Code via Tutorials on Repl.it!

← Back to all posts
Making a NodeJS Api
AcaiBerii (3)

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 some packages that you can install with NPM: HTTP, XMLHttpRequest and Querystring.

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:

const http = require('http');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const { parse } = require('querystring');

This introduces our NPM modules to our project.

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

const server = http.createServer((req, res) => {


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!

if (req.method === 'POST') {
  console.log("Valid request method!");
else {
  console.log("Invalid request method! We need a POST request, and not a " + req.method + " request!");

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.

let body = '';
req.on('data', chunk => {
    body += chunk.toString();

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!

req.on('end', () => {


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!

const reqparse = JSON.parse(body);

Now we can get parts of the JSON variable with:

const text-type = reqparse.text-type;

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:

if (text-type === 'JSON') {
  console.log("Valid request formatting!");

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

res.end('{"text-type":"JSON", "valid":"true"}');

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.

var KeepAliveAgent = require('keep-alive-agent'),
let agent = new KeepAliveAgent();

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

const MethodNotAllowed = http.STATUS_CODES[405];

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 do we need querystring? -- Querystring actually is useful for some APIs, it just depends on if you want to use it or not.
  • 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.


nbbcsf (23)

It would be so much easier to use express...

AcaiBerii (3)

@nbbcsf I know, but HTTP is similar. You can definitely try Express as well!