javascript - Change response body before outputting in node.js -
i'm new node.js. i'm tryinbg build small server acting proxy post call opendata service, doing stuff, binding presentation layer, outputting browser.
here's code:
dispatcher.onget("/metro", function(req, res) { var r = request({body: '<?xml version="1.0" encoding="iso-8859-1" ?><poirequest><poi_id>87087</poi_id><lng>0</lng></poirequest>'}, function (error, response, body) { if (!error && response.statuscode == 200) { console.log('public transformation public api called'); } }).pipe(res); res.on('finish', function() { console.log('request completed;'); }); }); http.createserver(function (req, res) { dispatcher.dispatch(req, res); }).listen(1337, '0.0.0.0'); console.log('server listening');
the dispatcher simplest found on mpm: https://npmjs.org/package/httpdispatcher question is: how can alter (basically, html-code stripping) response body before outputting output pipe?
you can use concat-stream accumulate of stream data , pass on callback can manipulate before returning browser.
var concat = require('concat-stream'); dispatcher.onget("/metro", function(req, res) { write = concat(function(completeresponse) { // here can modify resulting response before passing client. var finalresponse = modifyresponse(completeresponse); res.end(finalresponse); }); request('http://someservice').pipe(write); }); http.createserver(dispatcher.dispatch).listen(1337, '0.0.0.0'); console.log('server listening');
Comments
Post a Comment