npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

http-request-proxy

v0.5.0

Published

The middleware for proxying request in express

Downloads

9

Readme

http-request-proxy

It's a package aimed at proxy http request in expressjs. Using this package, you can setup a proxy server easily.

build status Test coverage David deps node version

NPM

Install

npm install http-request-proxy --save or yarn add http-request-proxy

How to use

Without body-parser

You treat you server as an transparent proxy in this mode, it doesn't modify any HTTP data (both in headers and body).

const express = require('express');
const path = require('path');

const {
    TIMEOUT_PROXY,
} = require('./config');
const requestRecordFilter = require('@yunnysunny/request-logging');
const proxy = require('http-request-proxy');

const app = express();
app.enable('trust proxy');

app.set('port', 3003);

app.use(requestRecordFilter());
app.use(proxy({
    urlsToProxy:{
        '/i/back': 'http://localhost:3004'
    },
    beforeParser: true,
    timeout: TIMEOUT_PROXY
}));

In this mode , you should set the parameter beforeParser to true. And you can set parameter timeout to an appropriate value you wanted in millisecond.

With body-parser

When you want to modify the request parameter in headers or body, you should use this mode. The dataPrepare is an function to modify the request parameters, which merged from querystirng and body. The headerPrepare is an function to modify the request headers, if it not given , the default headers configuration is {'User-Agent': 'http-proxy-server'}.

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const multipart = require('connect-multiparty');

const {
    HEADER_SEQ_NUM
} = require('./config');
const requestRecordFilter = require('@yunnysunny/request-logging');
const afterParserProxy = require('http-request-proxy');
const {doSign} = require('./helpers/auth_helper');

const app = express();
app.enable('trust proxy');

app.set('port', 3003);
app.use(requestRecordFilter());

app.use(bodyParser.json({limit: '1mb'}));
app.use(bodyParser.urlencoded({
    extended: false,
    limit: '1mb'
}));
app.use( multipart({}));
app.use(afterParserProxy({
    urlsToProxy:{
        '/i/back': 'http://localhost:3004',
    },
    dataPrepare: function(data) {
        data.sign = doSign(data);
        return data;
    },
    headerPrepare: function(req) {
        return {
            [HEADER_SEQ_NUM]: Number(req.get(HEADER_SEQ_NUM)) + 1
        };
    }
}));

But when you want to support uploading in this mode,you should add connect-multiparty to your dependencies, and write app.use( multipart({})) before call http-request-proxy.

It also can set the parameter of timeout to control how long you can bear before the response return.

Using an asynchronous function to determine the destination of backend

Some time you want to request a backend server with dynamic address, and you may request to a dispatcher server to get the backend's ip first. For this purpose, http-request-proxy supply a feature of setting the value of urlsToProxy to an an asynchronous function to determine the destination of backend.

const express = require('express');
const path = require('path');


const {
    slogger,
    TIMEOUT_PROXY,
    NOT_SUPPORT_URL
} = require('./config');
const requestRecordFilter = require('@yunnysunny/request-logging');
const afterParserProxy = require('http-request-proxy');

const app = express();
app.enable('trust proxy');

app.set('port', 3003);

app.use(requestRecordFilter());
app.use(afterParserProxy({
    urlsToProxy:{
        '/i/back': function(req) {
            return new Promise(function(resolve, reject) {
                if (req.path ==='/i/back' + NOT_SUPPORT_URL) {
                    return reject('not support');
                }
                resolve('http://localhost:3004');
            });
        }
    },
    beforeParser: true,
    timeout: TIMEOUT_PROXY
}));

Using custom configuration to override the warpper's

Sometime you may want to set a special configuration to certain backend server, the others use default setting. You can set one value of urlsToProxy to an object, the properties of the object will override the wrapper's.

const express = require('express');
const path = require('path');


const {
    slogger,
    TIMEOUT_PROXY,
    NOT_SUPPORT_URL
} = require('./config');
const requestRecordFilter = require('@yunnysunny/request-logging');
const afterParserProxy = require('http-request-proxy');

const app = express();
app.enable('trust proxy');

app.set('port', 3003);

app.use(requestRecordFilter());
app.use(afterParserProxy({
    urlsToProxy:{
        '/i/back': {
            url: function(req) {
                return new Promise(function(resolve, reject) {
                    if (req.path ==='/i/back' + NOT_SUPPORT_URL) {
                        return reject('not support');
                    }
                    resolve('http://localhost:3004');
                });
            },
            timeout: TIMEOUT_CUSTOM
        }
    },
    timeout: TIMEOUT_PROXY
}));

The timeout of /i/back will use TIMEOUT_CUSTOM, the others use TIMEOUT_PROXY.

API

See the document of api.

License

MIT