THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
Sometimes, the Application requires calling a Remote or external API from a nodejs Application.
It covers the following items
Nodejs is server-side code based on npm libraries.
Consume REST API involves HTTP request of type GET/POST/DELETE/PATCH
It involves sending a request of json data and receiving the HTTP Response with
For calling any rest API, We need the following.
http
and HTTPS
are inbuilt modules, that can be used as server and client
If the request url is https
, We can use the https
module, otherwise, use HTTP module
Here is a syntax to make an HTTP url request
http.get( options, response callback);
http.post( options, responsecallback);
required
keywordget
method with Url and returns call back, which contains data
and end
listenersdata listener called when API return response successfull.
const http = require('https');
const url = "https://jsonplaceholder.typicode.com/posts";
http.get(url, function (response) {
let posts = "";
response.on("data", function (data) {
posts += data.toString();
});
response.on("end", function () {
console.log(posts.length);
console.log(posts.toString());
});
});
🧮 Tags
Recent posts
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts