【问题标题】:client server communication using node.js使用 node.js 的客户端服务器通信
【发布时间】:2013-10-31 16:42:36
【问题描述】:

在我的客户端机器中,我有以下代码

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");

在我的服务器端机器中使用以下 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('');

在运行 node helper.js 时得到{ [Error: socket hang up] code: 'ECONNRESET' }

它不打印客户端中包含的 data.tostring()。

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    尝试在您的 switch 语句之前添加res.writeHead(200);

    此方法只能在消息上调用一次,并且必须在调用 response.end() 之前调用。

    来自http://nodejs.org/api/http.html#http_response_writehead_statuscode_reasonphrase_headers

    更新

    经过我们的讨论,以下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) {
            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");
    

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 2012-12-26
      • 2011-09-05
      • 2012-03-01
      • 1970-01-01
      • 2011-04-03
      相关资源
      最近更新 更多