Created
June 12, 2015 12:39
-
-
Save laggingreflex/eb30a31fd8e1e09a86cf to your computer and use it in GitHub Desktop.
Node forward path request to another server
This file contains 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
// Node forward path request to another server | |
// http://stackoverflow.com/questions/30800829/node-forward-path-request-to-another-server | |
var request = require('request'); | |
request = request.defaults({jar: true }); | |
app.all('/blog*', function(req, res, next) { | |
console.debug('==============='); | |
var jar = request.jar(); | |
var cookie = request.cookie(serializeCookie(req.session.cookies)); | |
var url = 'http://testwp' + req.url.slice(5); | |
jar.setCookie(cookie, url); | |
console.debug('start cookie:', jar); | |
var stream = request({ | |
method: req.method, | |
url: url, | |
form: req.body, | |
jar: jar, | |
}); | |
stream.pipe(res); | |
stream.on('end', function(){ | |
var cookies = jar.getCookies(url); | |
req.session.cookies = cookies; | |
console.debug('end cookies:', req.session.cookies); | |
console.debug('==============='); | |
}); | |
}); | |
function serializeCookie(cookie) { | |
var str = 'key=value;'; | |
if (cookie) | |
for (key in cookie) | |
str += key + '=' + cookie[key] + ';'; | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment