2

In my client machine i have the following code

client.js
    var fs      = require('fs');
    var http    = require('http');
    var qs      = require('querystring');
    var exec    = require('child_process').exec;
    var server = http.createServer(function(req, res) {
      switch(req.url) {
            case '/vm/list':
                getVms(function(vmData) {
                    res.end(JSON.stringify(vmData));
                });
                break;
            case '/vm/start':
                req.on('data', function(data) {
                    console.log(data.toString())
                    exec('CALL Hello.exe', function(err, data) {
                        console.log(err)
                        console.log(data.toString())
                        res.end('');
                    });
                });

                break;
        }
    });

    server.listen(9090);
    console.log("Server running on the port 9090");

in my server side machine am using following helper.js

var options = {
        host: '172.16.2.51',
        port: 9090,
        path: '/vm/start',
        method: 'POST'
    };

    var req = http.request(options, function(res) {
        res.on('data', function(d) {
            console.log(d.toString());
        });
    });

    req.on('error', function(e) {
        console.error(e);
    });


req.end('');

while running node helper.js am getting { [Error: socket hang up] code: 'ECONNRESET' }

it doesn’t print data.tostring() contained in the client side.

1 Answer 1

2

Try adding res.writeHead(200); before your switch statement.

This method must only be called once on a message and it must be called before response.end() is called.

From http://nodejs.org/api/http.html#http_response_writehead_statuscode_reasonphrase_headers.

Update

After our discussion the following client.js works:

var fs      = require('fs');
var http    = require('http');
var qs      = require('querystring');
var exec    = require('child_process').exec;
var server = http.createServer(function(req, res) {
  switch(req.url) {
        res.writeHead(200);
        case '/vm/list':
            getVms(function(vmData) {
                res.end(JSON.stringify(vmData));
            });
            break;
        case '/vm/start':
            req.on('data', function(data) {
                console.log(data.toString())
                exec('CALL Hello.exe', function(err, data) {
                    console.log(err)
                    console.log(data.toString())
                });
            });
            req.on('end', function() {
                res.end('');
            });

            break;
    }
});

server.listen(9090);
console.log("Server running on the port 9090");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.