Categories
JavaScript Answers

How to make synchronous requests in Node.js?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *