Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to send data to another server in node.js

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.25k
    Comment on it

    I have a situation in which I want to fetch data from another server. In node.js it is possible to send data to another server. For achieving this task we will use http module. To explain this I have written the code below .

    Example:

     

    var http = require('http');
    module.exports.saveMeeting = function(request, response){
    	var meetingData=request.body;
    	getMeetingRequestData(meetingData,function(callback){
    		response.json({
    	      Success: true,
    	      message: 'Meeting detail',
    	      meetingData:callback
    	    });	
    	});
    }
    
    //Function to send the http request and send the response back where we want.
    
    function getMeetingRequestData(meetingData,callback) {
    	http.get({
            host : 'localhost',
    	    port : 8000,
    	    path : '/meetingData', // the rest of the url with parameters if needed
    	    method : 'GET' // do GET
        }, function(res) {
        	
            // explicitly treat incoming data as utf8 (avoids issues with multi-byte chars)
            res.setEncoding('utf8');
    
            // incrementally capture the incoming response body
            var body = '';
            res.on('data', function(d) {
            	console.log(d);
                body += d;
            });
    
            // do whatever we want with the response once it's done
            res.on('end', function() {
                try {
                    var parsed = JSON.parse(body);
                } catch (err) {
                    console.error('Unable to parse response as JSON', err);
                    return callback(err);
                }
    
                // pass the relevant data back to the callback
                callback(parsed);
            });
        }).on('error', function(err) {
            // handle errors with the request itself
            console.error('Error with the request:', err.message);
            callback(err);
        });
    }
    
    module.exports.meetingData = function(request, response){
    	var meetingData= [{
            "appId": "111111",
            "userName": "rad",
            "password": "EBbA1xwVfgqYdEy3WvianNt+vK4=",
            "role": "admin"
          },
          {
            "appId": "111111",
            "userName": "rad",
            "password": "EBbA1xwVfgqYdEy3WvianNt+vK4=",
            "role": "admin"
          },]
    	response.json({
          Success: true,
          message: 'List of user',
          meetingData:meetingData
        });	
    }
    

     

    Here In the example we can see that we require http module then we write a POST http request in which all post meeting data we will get. Then we create a function named as getMeetingRequestData in which In first perameter we pass the post data which we get and in second perameter we will give the callback. In function getMeetingRequestData we will send http request by using http module and return the data to the callback.

    By this way we can send data to another server in node.js.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: