socket.js post请求中的socket挂起错误

时间:2014-02-20 08:42:36

标签: node.js parsing post https push

我正在尝试使用node.js将POST请求包装到Parse。我只想发送推送通知,但是套接字挂起错误不断出现。

这是我的代码:

var http = require('http')

var data = {
    channels: 'a_channel',
    data: {
            alert: "Something has happened"
    }
 }

 pdata = JSON.stringify(data);

 var apiKeys =  {
   "X-Parse-Application-Id": "lol",
   "X-Parse-REST-API-Key": "lolol",
   "Content-Type": "application/json",
    'Content-Length': pdata.length
};

 var options = {
    hostname: 'api.parse.com',
    port: 443,
    path: '/1/push',
    method: 'POST',
    headers: apiKeys
};


var req = http.request(options, function(res) {
res.setEncoding('utf-8');

var responseString = '';
 res.on('data', function(data) {
 responseString += data;
  });

 res.on('end', function() {
var resultObject = JSON.parse(responseString);
   });
   });

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


   req.write(pdata);
   req.end();
   console.log(pdata);

谢谢!

1 个答案:

答案 0 :(得分:0)

您正在使用http模块进行https呼叫。您应该在顶部需要https

另请注意pdata,因为它缺少 var 关键字。