node.js套接字在使用域时没有响应

时间:2017-08-04 15:45:56

标签: javascript node.js

我根据教科书示例输入,但没有来自客户的回复。它假设通过域方法弹出一些错误消息。但没有任何反应。

server.js:

var http = require('http');
var domain = require('domain');
http.createServer(function (req, res) {
    var reqd = domain.create();
    reqd.add(req);
    reqd.add(res);
    reqd.on('error', function (err) {
        res.writeHead(200);
        res.write('server got error when responsing to client request ');
        res.end(err.message);
    });
    res.writeHead(200);
    res.on('data', function () {
        nonexist();
        res.write('hello');
        res.end();
    });

}).listen(1337);

client.js

var http = require('http');
var options = {
    hostname: 'localhost',
    port: 1337,
    path: '/',
    method: 'POST',
};
var req = http.request(options, function (res) {
    res.setEncoding('utf-8');
    res.on('data', function (chunk) {
        console.log('response content: ' + chunk);
    });
});
req.write('hello');
req.end('byebye');

0 个答案:

没有答案