Scoped HTTP Client for Node.js
Scoped HTTP Client provides a fluent, chainable API wrapper around Node.js's native `http` client, simplifying common HTTP request patterns. Instead of handling numerous optional arguments directly on `http.request`, it allows developers to build requests by chaining methods like `.header()`, `.path()`, `.query()`, and `.get()`/`.post()`. A key feature is its 'scoping' mechanism, enabling temporary modifications to client parameters for specific requests without altering the base client instance. The package's current stable version is 0.11.0, last published in 2015. Given its age and an `engines` declaration targeting `node >= 0.8.0`, it is no longer actively maintained and is likely incompatible with modern Node.js environments. Its approach to request building has largely been superseded by contemporary HTTP clients like Axios, Got, or the native Fetch API, which offer more robust features, Promises/async-await support, and active maintenance.
Common errors
-
Error: Cannot find module 'scoped-http-client'
cause The package has not been installed or is not correctly listed in `package.json`.fixRun `npm install scoped-http-client` in your project directory. -
TypeError: client.get is not a function
cause This error likely means `client.get()` was called without the final `()` to execute the request builder, or `client` was not correctly initialized from `scopedClient.create()`.fixEnsure the HTTP method call is immediately invoked to trigger the request, e.g., `client.get()(function(err, resp, body){...})`. -
Error: connect ECONNREFUSED <IP_ADDRESS>:<PORT>
cause The client could not establish a connection to the target server, often due to the server being offline, incorrect host/port, or firewall issues.fixVerify the target URL, port, and ensure the server is running and accessible from the client's environment.
Warnings
- breaking This package targets Node.js version 0.8.0 and has not been updated since 2015. It is highly probable that it is incompatible with modern Node.js runtimes (e.js., Node.js v10+), especially due to changes in Node.js's internal `http` module and removal of deprecated APIs like `http.createClient` in Node.js v0.10.0.
- gotcha The API is entirely callback-based, meaning it does not support Promises, `async/await`, or modern error handling patterns. This can lead to callback hell and make integration with contemporary JavaScript codebases challenging.
- gotcha The package is unmaintained, meaning it will not receive updates for security vulnerabilities, bug fixes, or performance improvements. Using abandoned libraries can introduce significant security risks into an application's dependency tree.
- deprecated The `util.puts` function, used in the quickstart and README examples, is a deprecated Node.js API. It behaves similarly to `console.log` but with some historical differences in output buffering and newline handling.
- gotcha This client lacks advanced features common in modern HTTP libraries, such as automatic JSON parsing, request retries, rate limiting, interceptors, or comprehensive timeout configurations, making it less suitable for complex or resilient applications.
Install
-
npm install scoped-http-client -
yarn add scoped-http-client -
pnpm add scoped-http-client
Imports
- scopedClient
import scopedClient from 'scoped-http-client';
const scopedClient = require('scoped-http-client'); - create
const client = require('scoped-http-client').create('https://api.example.com');const client = scopedClient.create('https://api.example.com'); - Client Methods (.get, .post, .header, .path)
client.get(callback); // Missing () to execute the request builder
client.header('Accept', 'application/json').get()(callback);
Quickstart
const scopedClient = require('scoped-http-client');
const util = require('util'); // Node.js built-in module
// Example 1: Basic GET request
const githubClient = scopedClient.create('https://api.github.com')
.header('accept', 'application/json')
.path('users/technoweenie')
.get()(function(err, resp, body) {
if (err) {
console.error('Error fetching user:', err);
return;
}
console.log('GET Response Status:', resp.statusCode);
util.puts('User Info: ' + body);
});
// Example 2: POST request within a scope
const token = process.env.GITHUB_TOKEN ?? 'YOUR_DUMMY_GITHUB_TOKEN'; // Replace with a real token or handle auth
const scopedBaseClient = scopedClient.create('https://api.github.com')
.query({ token: token });
scopedBaseClient.scope('user/repos', function(cli) {
const data = JSON.stringify({ name: 'my-new-repo', description: 'A test repository' });
cli.post(data)(function(err, resp, body) {
if (err) {
console.error('Error creating repo:', err);
return;
}
console.log('POST Response Status:', resp.statusCode);
util.puts('Repo creation response: ' + body);
});
});