我试图创建一个脚本来触发IFTTT通知。 到目前为止我的工作是:
var http = require('http')
var body = JSON.stringify({
value1: "Temp Humid Sensor",
value2: "Error",
value3: "reading measurements"
})
var sendIftttTNotification = new http.ClientRequest({
hostname: "maker.ifttt.com",
port: 80,
path: "/trigger/th01_sensor_error/with/key/KEY",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(body)
}
})
sendIftttTNotification.end(body)
但我想做的是创建一个可重用的函数,以便在不同的情况下用不同的参数调用它。
到目前为止我想出了什么:
var http = require('http')
function makeCall (body, callback) {
new http.ClientRequest({
hostname: "maker.ifttt.com",
port: 80,
path: "/trigger/th01_sensor_error/with/key/UMT-x9TH83Kzcq035sh9B",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(body)
}
}
var body1 = JSON.stringify({
value1: "Sensor",
value2: "Error",
value3: "reading measurements"
})
makeCall(body1);
var body2 = JSON.stringify({
value1: "Sensor",
value2: "Warning",
value3: "low battery"
})
makeCall(body2);
但是没有任何反应,当我跑步时我没有收到任何错误:" node script.js"在终端
有人可以帮我这个吗?
谢谢!
答案 0 :(得分:1)
您的功能正在发出请求但未发送请求。
试试这个:
function makeCall (body, callback) {
var request = new http.ClientRequest({
hostname: "maker.ifttt.com",
port: 80,
path: "/trigger/th01_sensor_error/with/key/UMT-x9TH83Kzcq035sh9B",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(body)
});
request.end(body);
callback();
}