简体   繁体   English

node.js发布请求中的套接字挂起错误

[英]socket hang up error in node.js post request

I'm trying to wrap a POST request to Parse using node.js. 我正在尝试使用node.js将POST请求包装为Parse。 I just want to send a Push notification, but a socket hang up error keeps showing up. 我只想发送一个Push通知,但是套接字挂起错误不断出现。

And this is my code: 这是我的代码:

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

Thanks! 谢谢!

You are using the http module for doing an https call. 您正在使用http模块进行https调用。 You should require the https at the top. 您应该在顶部要求https

Also be careful with pdata as it's missing the var keyword. 还请小心pdata因为它缺少var关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM