Node.js HTTP Request Libraries: Six Methods, Code Samples, and Comparison
This article introduces six different methods for making HTTP requests in Node.js, demonstrates each with code examples using the Juejin API, compares their features, installation, and popularity, and provides a concluding recommendation on preferred libraries.
This article presents six ways to perform HTTP requests in Node.js, using the Juejin community category API as a demonstration. It first installs the chalk library to colorize console output.
Node.js HTTPS Module
The built‑in https module requires no external dependencies and can handle simple requests.
const chalk = require("chalk")
const https = require('https')
https.get('https://api.juejin.cn/tag_api/v1/query_category_briefs', res => {
let list = [];
res.on('data', chunk => { list.push(chunk); });
res.on('end', () => {
const { data } = JSON.parse(Buffer.concat(list).toString());
data.forEach(item => {
console.log(`${chalk.yellow.bold(item.rank)}.${chalk.green(item.category_name)}`);
})
});
}).on('error', err => {
console.log('Error: ', err.message);
});Axios
Axios is a popular Promise‑based HTTP client that works both in browsers and Node.js, offering interceptors and automatic JSON conversion.
npm i -S axios const chalk = require("chalk")
const axios = require('axios');
axios.get('https://api.juejin.cn/tag_api/v1/query_category_briefs')
.then(res => {
const { data } = res.data;
data.forEach(item => {
console.log(`${chalk.yellow.bold(item.rank)}.${chalk.green(item.category_name)}`);
})
})
.catch(err => {
console.log('Error: ', err.message);
});Got
Got is a human‑friendly, feature‑rich HTTP request library that supports Promise API, HTTP/2, pagination, and caching.
npm i -S [email protected] const chalk = require("chalk")
const got = require('got');
got.get('https://api.juejin.cn/tag_api/v1/query_category_briefs', { responseType: 'json' })
.then(res => {
const { data } = res.body;
data.forEach(item => {
console.log(`${chalk.yellow.bold(item.rank)}.${chalk.green(item.category_name)}`);
})
})
.catch(err => {
console.log('Error: ', err.message);
});Needle
Needle is a lightweight library offering both callback and Promise interfaces, with automatic XML/JSON conversion.
npm i -S needle const chalk = require("chalk")
const needle = require('needle');
needle.get('https://api.juejin.cn/tag_api/v1/query_category_briefs', (err, res) => {
if (err) return console.log('Error: ', err.message);
const { data } = res.body;
data.forEach(item => {
console.log(`${chalk.yellow.bold(item.rank)}.${chalk.green(item.category_name)}`);
})
})Superagent
Superagent is an early, progressive HTTP client with a chainable API similar to Axios, but requires manual JSON parsing.
npm i -S superagent const chalk = require("chalk")
const superagent = require('superagent');
superagent.get('https://api.juejin.cn/tag_api/v1/query_category_briefs')
.then(res => {
const { data } = JSON.parse(res.text);
data.forEach(item => {
console.log(`${chalk.yellow.bold(item.rank)}.${chalk.green(item.category_name)}`);
})
})
.catch(err => {
console.log('Error: ', err.message);
});node‑fetch
node‑fetch mirrors the browser fetch API, providing a familiar Promise‑based interface; version 2.6.7 is used for compatibility.
npm i -S [email protected] const chalk = require("chalk")
const fetch = require('node-fetch');
fetch('https://api.juejin.cn/tag_api/v1/query_category_briefs', { method: 'GET' })
.then(async res => {
let { data } = await res.json();
data.forEach(item => {
console.log(`${chalk.yellow.bold(item.rank)}.${chalk.green(item.category_name)}`);
})
})
.catch(err => {
console.log('Error: ', err.message);
});Comparison
The article shows a download‑trend chart for the past year, indicating that node-fetch has the highest downloads while needle is the least popular. It also lists stars, versions, unpacked sizes, and creation years for each library, highlighting Axios’s dominant star count.
Conclusion
All libraries can perform HTTP requests, but the author prefers Axios for its convenience and familiarity, while noting the growing interest in node-fetch due to its small size and familiar API.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.