Summary
I'm creating a program to send a request to the HTTP endpoint as follows.
- send a HTTP request by POST
- receive response data
- HTTP request by PUT at the ID and time received by POST during POST response.
- receive response data
code
A nodejs in the making
'use strict'
const http = require('http');
const HOST = `hoge.io`;
const PATH = `/path`;
let postData = {
"name": "stack",
};
let postDataStr = JSON.stringify(postData);
let options = {
host: HOST,
port: 80,
path: PATH,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postDataStr)
}
};
let req = http.request(options, (res) => {
let data = '';
let body = '';
console.log(`POST statusCode: ${res.statusCode}`)
res.setEncoding('utf8');
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
let resultData = '';
data = JSON.parse(body);
let now = new Date().getTime();
let time = data.actives_at - now;
// setTimeout(putData.bind(this, data), time);
console.log(data)
});
});
req.on('error', (e) => {
console.log('error: ' + e.message);
});
req.write(postDataStr);
req.end();
response value
node api.js
{
id: 'xxxxxxxxxxx',
actives_at: xxxxxxxxxx (in UNIX time milliseconds),
called_at: xxxxxxxxxx,
}
What I want to do.
When this reaches the time of actsives_at, we want to send the request to the endpoint with a PUT as shown below.
$ curl -X 'PUT' -H 'X-Id:xxxxxxxxxx' http://hoge.io/path
What additions to the code would I need to make to make this happen?
You can consider using AgendaJS (https://github.com/agenda/agenda), a scheduler using Mongodb to store your data. It will accept a Date object or a timestamp or even a MomentJS object and will run a function you define when the time come