Giter VIP home page Giter VIP logo

node-http-proxy-json's Introduction

node-http-proxy-json Build Status

for node-http-proxy transform the response json from the proxied server.

Installation

npm install node-http-proxy-json

Motivation

When using node-http-proxy need to modify the response. If your proxy server returns HTML/XML document, you can try Harmon. But sometimes the proxy server only returns the JSON. For example, call API from the server. Usually the server will compress the data. So before using this repository, confirm your server compression format, currently only supports gzipใ€deflate and uncompressed. If you need other compression formats, please create a new Issue, and I will try to achieve it as much as possible.

Use Cases

Simulation server using gzip compression

var zlib = require('zlib');
var http = require('http');
var httpProxy = require('http-proxy');
var modifyResponse = require('node-http-proxy-json');

// Create a proxy server
var proxy = httpProxy.createProxyServer({
    target: 'http://localhost:5001'
});

// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function (proxyRes, req, res) {
    modifyResponse(res, proxyRes, function (body) {
        if (body) {
            // modify some information
            body.age = 2;
            delete body.version;
        }
        return body; // return value can be a promise
    });
});

// Create your server and then proxies the request
var server = http.createServer(function (req, res) {
    proxy.web(req, res);
}).listen(5000);

// Create your target server
var targetServer = http.createServer(function (req, res) {

    // Create gzipped content
    var gzip = zlib.Gzip();
    var _write = res.write;
    var _end = res.end;

    gzip.on('data', function (buf) {
        _write.call(res, buf);
    });
    gzip.on('end', function () {
        _end.call(res);
    });

    res.write = function (data) {
        gzip.write(data);
    };
    res.end = function () {
        gzip.end();
    };

    res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'gzip'});
    res.write(JSON.stringify({name: 'node-http-proxy-json', age: 1, version: '1.0.0'}));
    res.end();
}).listen(5001);

Simulation server using deflate compression

var zlib = require('zlib');
var http = require('http');
var httpProxy = require('http-proxy');
var modifyResponse = require('../');

// Create a proxy server
var proxy = httpProxy.createProxyServer({
    target: 'http://localhost:5001'
});

// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function (proxyRes, req, res) {
    modifyResponse(res, proxyRes, function (body) {
        if (body) {
            // modify some information
            body.age = 2;
            delete body.version;
        }
        return body; // return value can be a promise
    });
});

// Create your server and then proxies the request
var server = http.createServer(function (req, res) {
    proxy.web(req, res);
}).listen(5000);

// Create your target server
var targetServer = http.createServer(function (req, res) {

    // Create deflated content
    var deflate = zlib.Deflate();
    var _write = res.write;
    var _end = res.end;

    deflate.on('data', function (buf) {
        _write.call(res, buf);
    });
    deflate.on('end', function () {
        _end.call(res);
    });

    res.write = function (data) {
        deflate.write(data);
    };
    res.end = function () {
        deflate.end();
    };

    res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'deflate'});
    res.write(JSON.stringify({name: 'node-http-proxy-json', age: 1, version: '1.0.0'}));
    res.end();
}).listen(5001);

Server does not enable compression

var http = require('http');
var httpProxy = require('http-proxy');
var modifyResponse = require('../');

// Create a proxy server
var proxy = httpProxy.createProxyServer({
    target: 'http://localhost:5001'
});

// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function (proxyRes, req, res) {
    modifyResponse(res, proxyRes, function (body) {
        if (body) {
            // modify some information
            body.age = 2;
            delete body.version;
        }
        return body; // return value can be a promise
    });
});

// Create your server and then proxies the request
var server = http.createServer(function (req, res) {
    proxy.web(req, res);
}).listen(5000);

// Create your target server
var targetServer = http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'deflate'});
    res.write(JSON.stringify({name: 'node-http-proxy-json', age: 1, version: '1.0.0'}));
    res.end();
}).listen(5001);

Tests

To run the test suite, first install the dependencies, then run npm test:

$ npm install
$ npm test

License

MIT

node-http-proxy-json's People

Contributors

langjt avatar taras avatar homkai avatar canda avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.