-
-
Save b9AobJ/eb836f8fa79236132ff5062c22d9890b 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* 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 <[email protected]> | |
******************************************************************************/ | |
'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'); | |
}); | |
//////////////////////////////////////////////////////////////////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment