听取https请求,然后收到发送另一个请求

时间:2015-05-16 03:43:10

标签: node.js post https request

我正在尝试建立一个物联网网络。

在我的节点服务器上,我正在侦听WAN / LAN HTTPS请求。收到后,我希望它向独立的物联网发送HTTP请求。我举了一个简单的例子:

var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};

var a = https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8000);

有人可以向http://192.168.1.137/?pin=ON1发送一个http POST请求,以及上面代码中的“hello world”吗?

2 个答案:

答案 0 :(得分:0)

docs for http.request包含如何发送POST请求的示例。在这里,它被修改为发送您要求的POST请求。

var postData = querystring.stringify({
  'msg' : 'Hello World!'
});

var options = {
  hostname: '192.1681.137',
  port: 80,
  path: '/?pin=ON1',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write(postData);
req.end();

答案 1 :(得分:-2)

#include <http://192.168.1.137/?pin=ON1>
requesting=true    
var https = require('https');
    var fs = require('fs');

    var options = {
      key: fs.readFileSync('key.pem'),
      cert: fs.readFileSync('cert.pem')
    };

    cout << "hello world">
    var a = https.createServer(options, function (req, res) {
      res.writeHead(200);
      res.end("hello world\n");
    }).listen(8000);
相关问题