Functional API in Node.js without external libraries. 👦

Pablo Ruiz Ponce
2 min readDec 3, 2020

👦 Junior developer: To understand this post you don’t need to be an expert but you must be familiar with concepts like: Node.js, API and HTTP.

The motivation for writing this post is because this would have been very useful for me a month back. I’m enrolled in a subject called Web Services and Middleware and on this subject we were supposed to create an API from scratch.

We were free to use any programing language, but there was a restriction: “If you use Node.js, it is not allowed to use Express”

At first, I thought that this task was going to be very easy, but after a few minutes surfing the web I’ve realized that I was wrong. Most of the examples that you can find are based on Express and the few ones that use the HTTP library were extremely basic. This post helped me a lot but it still not enough.

The complete code is right below and during this post I will decompose it and I will explain each part.

This API is based on a customer service. The end users are able to get all the customers and their orders, get detailed information from an specific customer and their orders, add customers and add orders.

The first part of our code must be the HTTP Server where the API will be running. The server has a callback function inside where we will handle everything. This function has 2 parameters:

  • req - The HTTP request of the client
  • res - The HTPP response that the server will provide to the client

Now, if you execute your code, the server will be running in your localhost using the port 3000. At this point, if we go to to localhost:3000/ you won’t see anything. This is because we don’t have the logic of the API yet.

After setting up the server, I like to define all the routes and methods allowed by the API. In Express the routing process is absurdly easy. On the other hand, using just the http library makes the things a little bit more trickier.

For the endpoints the best solution and the one I use is regex. You can also use just the url of the request, as I do on the root, but you will have troubles when defining more complex routes.

For the allowed HTTP methods I use the parameter method of the request

Now your API is almost ready. The last thing you have to do is adding all the rest of the logic, the headers of the response and the actual response of the server. For doing that you have the following methods and attributes:

  • res.statusCode - Attribute to set the status code of the response
  • res.setHeader() - Method to set HTTP headers on the response
  • res.write() - Method to add response data
  • res.end() - Method to end the response and send it to the client

That’s all for my first post in Medium. I hope everything of the explained is clear. If you have any doubt or you think that something can be improved in my implementation, write a comment please.

--

--

Pablo Ruiz Ponce
0 Followers

Computer Science Student in the University of Alicante