如何通过http从nodejs中的服务器上的客户端接收数据

时间:2015-09-05 08:57:04

标签: node.js

这是我的两个文件,我无法打印"您好客户"来自客户端,js,但来自server.js的消息可以在我的客户终端上轻松打印

// client.js
var fs = require('fs');
var https = require('https');
var options = {
    key: fs.readFileSync('client_key.pem'),
    cert: fs.readFileSync('client_cert.pem'),
    requestCert: false,
    rejectUnauthorized: false,
    host: '0.0.0.0',
    port: 3000,
    method: 'GET',
    path: '/',
    agent: false 
};
var request = https.request(options, function(response) {
    console.log('response.statusCode:', response.statusCode);
    request.write("hi i m client");
    //response.write('Hi i m res client');
    //response.pipe(my);
    response.on('data', function(data) {
        console.log('got some data back from the server:' + data);
    });
});
request.end();

这是我的server.js:

// my server.js
var fs = require('fs');
var https = require('https');
var options = {
    host: '0.0.0.0',
    port: 3000,
    key: fs.readFileSync('server_key.pem'),
    cert: fs.readFileSync('server_cert.pem'),
    requestCert: false,
    rejectUnauthorized: false
};
var server = https.createServer(options, function(req, res) {
    //console.log('inside the server::',req.socket);
    req.on('data', function(data) {
        console.log('recieved::' + data);
    });
    res.writeHead(200, {
        'Content-Type': 'text/plain'
    });
    res.write("hi i mm server");
    res.end();
});
server.listen(3000);

0 个答案:

没有答案