How to make synchronous requests in Node.js?

Sometimes, we want to make synchronous requests in Node.js.

In this article, we’ll look at how to make synchronous requests in Node.js.

How to make synchronous requests in Node.js?

To make synchronous requests in Node.js, we can use the sync-request package.

To install it, we run

npm i sync-request

Then we use it by writing

const request = require("sync-request");

try {
  const res = request("GET", url);
  const uComp = res.split("n")[1].split(", ")[1];
  doSomething(uComp);
} catch (e) {}

to call request with 'GET' and url to make a GET request to the url.

Then we get the response with res.

Conclusion

To make synchronous requests in Node.js, we can use the sync-request package.