Socket
Book a DemoInstallSign in
Socket

http-request-proxy

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-request-proxy

The middleware for proxying request in express

0.5.0
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

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

Keywords

proxy

FAQs

Package last updated on 30 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.