Atharva Deosthale Web Developer and Designer | JavaScript = ❤ | MERN Stack Developer

Using Needle to send HTTP requests in Node.js

3 min read 1049

While dealing with backends, there are situations in which we need to make HTTP requests to other APIs for information. There are a lot of different packages in Node.js that help with that, such as node-fetch and Axios.

Today we are going to look at another package called Needle, and see why we should use it instead of something like Axios.

Needle is a HTTP client made for Node that helps us send HTTP requests to external sources to retrieve data. This should be used in the backend in situations where you don’t want to expose sensitive information such as API keys to the client application.

Let’s see how this package actually works. In this tutorial, we will be using a fake REST API (JSONPlaceholder) to serve data through our server with Needle to make the HTTP request.

Why you should use Needle

The reason I would choose Needle rather than Axios is that Needle is made for the backend, while Axios is made for both the frontend and the backend. Not that Axios is a bad package; I’ve used both of the packages quite a lot and both of them are awesome. But Needle’s specific use case resides in the backend, as it’s tagline reads, “the leanest and most handsome HTTP client in the Nodelands”.

Needle is made out of Node-specific libraries, which means it won’t work in the browser at the frontend. If we take a closer look at the User-Agent header Needle sets by default in its requests, it’s set to the running Node platform and version. This means that Needle is meant to run on Node and uses smaller-sized Node packages to perform requests. This makes this package less bloated for backend.

Also, Needle’s package size is smaller in comparison to Axios. While package size may not matter as much these days, smaller package sizes like Needle are great for optimization purposes. If package size is a concern for you, it makes sense to use Needle in the backend and Axios in the frontend.

When you are working with the backend, you know for sure that the code is not going to be used in the frontend in any way, so why not save some space and use Needle instead of Axios?

Using Axios and Needle is quite similar. You won’t find performance differences in a normal usage, but when you really have to optimize things to make your server faster, using lightweight packages like Needle is a good option.

Creating a sample Node.js server

Let’s see Needle in action. Our first step is to set up a sample Node and Express server.

We made a custom demo for .
No really. Click here to check it out.

To do that, go to a safe project workspace folder and type the following command to set up the package.json for our project:

npm init -y

Now let’s install the Needle and Express packages like so:

npm install needle express

Next, create a new file called index.js and use node index in your terminal to run it. I highly recommend using nodemon, because it auto reloads the server once you make changes to a file.

Install it using the following command (you need to install it just once per machine):

npm install -g nodemon

Setting up a sample Express server

Now, go to the index.js file, and use the following code to set up a bare bones Express app. We will also import Needle here for further use:

const express = require("express");
const needle = require("needle");
const app = express();
const PORT = process.env.PORT || 8080;

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

In the code above, process.env.PORT || 8080 means that if there is no port provided in environment variables, the server will run on port 8080. This is quite useful when you are deploying on services like Heroku and you need some way to test the server locally.

Setting up a route and working with Needle

Now let’s create a route that, when sent a request, will return a response from a fake API. This will be a simple GET request:

app.get("/posts", async (req, res) => {
  try {
    const response = await needle(
      "get",
      "https://jsonplaceholder.typicode.com/posts"
    );
    console.log(response.body);
    res.json(response.body);
  } catch (err) {
    console.log(err);
    res.status(500).json({ message: "Internal server error" });
  }
});

Let’s look at what are we doing in the above code. First, we are setting up an Express route at the route /posts and using an asynchronous callback function. Then, we are making a GET request from Needle, and specifying the URL to which we are sending the HTTP request.

Our response is stored in response.body and we send this response back to the user using res.json().

We use the catch block if anything goes wrong in making the request, and send an error message as a response if so.

Now, if you hit http://localhost:8080/posts using an HTTP client like Postman or simply using a browser, you will get a response with a lot of dummy posts in JSON format.

We can do many more things, like checking the HTTP status code. Let’s return the status code we got from the request back as a response:

res.status(response.statusCode).json(response.body);

In the above snippet, we are just adding the .status() method from express to return a status code as a response.

Similarly, you can pass headers to requests to send request data such as API Keys

const response = await needle(
  "get",
  "https://jsonplaceholder.typicode.com/posts",
  { headers: { "x-my-custom-key": "abc123" } }
);

And you can parse response headers like this:

console.log(response.headers);

Conclusion

Needle has been around for many years, even before Axios. Make sure to try it out once for yourself! More and more people are starting to use Needle this year, which indicates that devs want more optimization. The first step towards it is using packages with smaller sizes.

However, if you are just a student or you are working on a simple project that does not necessarily require optimization, I can’t see a difference between using Axios over Needle. I think the optimization part is more useful in large scale, production-level apps where developers need to keep things as clean as possible.

200’s only Monitor failed and slow network requests in production

Deploying a Node-based web app or website is the easy part. Making sure your Node instance continues to serve resources to your app is where things get tougher. If you’re interested in ensuring requests to the backend or third party services are successful, try LogRocket. https://logrocket.com/signup/

LogRocket is like a DVR for web and mobile apps, recording literally everything that happens while a user interacts with your app. Instead of guessing why problems happen, you can aggregate and report on problematic network requests to quickly understand the root cause.

LogRocket instruments your app to record baseline performance timings such as page load time, time to first byte, slow network requests, and also logs Redux, NgRx, and Vuex actions/state. .
Atharva Deosthale Web Developer and Designer | JavaScript = ❤ | MERN Stack Developer

Leave a Reply