Node.js HTTP Module for Browsers (Browserify)

1.7.0 · maintenance · verified Tue Apr 21

http-browserify provides a browser-compatible implementation of Node.js's native `http` module, specifically designed to be used with Browserify. When bundling browser-side code with Browserify, this package allows `require('http')` calls to function correctly, enabling HTTP requests from the browser environment using a Node.js-like API. The current stable version is 1.7.0. This package is part of the broader Browserify ecosystem, which focuses on bringing Node.js modules to the browser. It implements core HTTP client functionalities such as `http.request`, `http.get`, and methods for managing headers and data streams. Key differentiators include its tight integration with Browserify's module resolution and its effort to mimic the Node.js API, making it easy for developers to port server-side HTTP logic to the client.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to perform a basic GET request using the `http` module API in a Browserify-bundled environment, handling data and end events.

const http = require('http');

const resultDiv = document.createElement('div');
resultDiv.id = 'result';
document.body.appendChild(resultDiv);

// Mock server response for demonstration in a real browserify context
// In a true browser environment, this would hit an actual endpoint.
const mockEndpoint = '/beep';

console.log(`Making a GET request to ${mockEndpoint}`);

http.get({ path : mockEndpoint }, function (res) {
    resultDiv.innerHTML += `GET ${mockEndpoint}<br>`;
    
    res.on('data', function (buf) {
        resultDiv.innerHTML += `Received data: ${buf}<br>`;
    });
    
    res.on('end', function () {
        resultDiv.innerHTML += '<br>__END__';
        console.log('Request ended.');
    });

    res.on('error', function(err) {
        resultDiv.innerHTML += `Error: ${err.message}<br>`;
        console.error('Request error:', err);
    });
});

// To make this runnable without a backend, you'd typically intercept the request
// For a real-world scenario, '/beep' would be served by a backend.
// This example demonstrates the client-side API usage.

view raw JSON →