Skip to content

Instantly share code, notes, and snippets.

@NickNaso
Last active January 25, 2023 18:40
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save NickNaso/96aaad34e305823b9ff6ba3909908f31 to your computer and use it in GitHub Desktop.
Save NickNaso/96aaad34e305823b9ff6ba3909908f31 to your computer and use it in GitHub Desktop.
Log or modify the request body in the node-http-proxy before to pass it to the express.js application
/*******************************************************************************
* Copyright (c) 2017 Nicola Del Gobbo
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the license at http://www.apache.org/licenses/LICENSE-2.0
*
* THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
* IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
* MERCHANTABLITY OR NON-INFRINGEMENT.
*
* See the Apache Version 2.0 License for specific language governing
* permissions and limitations under the License.
*
* Contributors - initial API implementation:
* Nicola Del Gobbo <nicoladelgobbo@gmail.com>
******************************************************************************/
'use strict';
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const httpProxy = require('http-proxy');
const morgan = require('morgan');
/////////////////////////////////// PROXY SERVER ///////////////////////////////
const proxy = httpProxy.createProxyServer({});
// Restream parsed body before proxying
proxy.on('proxyReq', function(proxyReq, req, res, options) {
if(req.body) {
let bodyData = JSON.stringify(req.body);
// In case if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq.setHeader('Content-Type','application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
// Stream the content
proxyReq.write(bodyData);
}
});
const proxyApp = express();
proxyApp.use(bodyParser.json());
proxyApp.use(bodyParser.urlencoded({extended: true}));
proxyApp.use(function(req, res){
// ... do some stuff
// ... log your body and something else
console.log('proxy body:',req.body)
proxy.web(req, res, {
target: 'http://127.0.0.1:5000'
})
});
http.createServer(proxyApp).listen(8080, '0.0.0.0', () => {
console.log('Proxy server linsten on 8080');
});
/////////////////////////////////// PROXY SERVER ///////////////////////////////
////////////////////////// YOUR APP BEHIND PROXY ///////////////////////////////
const app = express();
app.use(morgan('dev'));
app.use(bodyParser.json());
//app.use(bodyParser.urlencoded({extended: true}));
app.post('/post', (req, res) => {
console.log("App body => ", req.body);
res.send('POST REQUEST');
});
app.get('/get', (req, res) => {
res.send('GET REQUEST');
});
http.createServer(app).listen(5000, '127.0.0.1', () => {
console.log('Applicaton server linsten on 5000');
});
////////////////////////////////////////////////////////////////////////////////
@mosfet1kg
Copy link

Thanks a lot!!!!!!!

@NickNaso
Copy link
Author

NickNaso commented Sep 5, 2019

I'm happy that this was helpful to you. :-)

@daedalius
Copy link

You have literally saved me.
Thanks, men!

@daedalius
Copy link

A little addition.
As far as I understood that "proxy.on proxyReq" handler should be the last. In another case, you will see an error like "cant modify request because it was sent" while modifying request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment