How to make HTTP Request in Node.js

By

I’ve come up with Node.js helper to make http request in Node.js. Code uses native Node.js http module to perform requests which is extremely simple yet very handy for quick testing.

Following Helper (helper/http.js) is responsible of performing http request and handover the response body to the callback or throws exception on error. Purpose of this Node.js http helper is to promote reusability. Please note exception/error handling is not covered in this article.

So, How do I make basic HTTP Request?

GET/POST Request

Envato API Consumer – Example

As an another example, we are going to perform a GET request to Envato API with user-agent header to fetch number-of-files available on Themeforest (which returns categories and their count) and prints the result to the console.

 Summary

As mentioned, this is a simplest example of creating an HTTP request with the native Node.js code! You can add more features such as response status codes validation, follow chain of redirects etc