node.js - NodeJS data throughput -


i've set nodejs server can accessed client. every once in while it's necessary let server connect second server , feed information retrieved client.

connecting second server easy part, honest have no idea how send client. res.write seems forbidden during connection second server.

the connection client handled handlegetrequest. connection second server starts @ http.get.

var http = require('http'); var url  = require('url');  var server = http.createserver(function(req, res) {   var url_parsed = url.parse(req.url, true);    if (req.method ==='get') {     handlegetrequest(res, url_parsed);   } else {     res.end('method not supported');   } });   handlegetrequest = function(res, url_parsed) {   if (url_parsed.path == '/secondary') {     var options = {       hostname: "localhost",       port: "8900",       path: "/from_primary"     }      http.get(options, function(secget) {       resget.on('data', function(chunk) {        // either store 'chunk' later use or send directly        });     }).on('error', function(e) {       console.log("error " + e.message);     });    } else {     res.writehead(404);   }   res.end('closed'); }; server.listen(8000); 

how send chunk http.request client?

i thinks passing callback handlegetrequest fix issue:

if (req.method === 'get') {   handlegetrequest(url_parsed, function (err, response) {     if (err) {       return res.sendstatus(500);     }      res.json(response);   }); } else {   res.end('method not supported'); }  handlegetrequest = function (url_parsed, callback) {   // options ...    http.get(options, function(resget) {     var data = '';      resget.on('data', function(chunk) {       data += chunk;     });      resget.on('end', function() {       callback(null, data);     });   }).on('error', function(e) {     callback(e);   }); } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -