@jajoosam/

timer-test

Nodejs

testing kron.fun

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const app = require('express')();
const axios = require('axios');
const chalk = require('chalk').constructor({ enabled: true, level: 3 });

app.get(`/`, (req, res) => {
  res.send(`Empty!`)
})

let cycles = 0;

app.get("/cycles", (req, res) => {
  res.send(cycles);
})
// start off the cycle by going to /ping
app.get(`/ping`, (req, res) => {
  if (req.headers["user-agent"].includes("kron")) {
    cycles -= 1;
  }
  let next = new Date();
  if (cycles <= 1) {
    // kron includes the unique ID in the user agent
    console.log(`${chalk.cyan("=>")} ${chalk.magenta(next.toUTCString())} ${chalk.blue(req.headers["user-agent"])}`);

    // set the date for he next request
    next.setSeconds(next.getSeconds() + 60);
    axios.post(`https://kron.fun/new`,
      {
        date: next.toUTCString(),
        url: "https://timer-test--jajoosam.repl.co/ping",
        method: "GET"
      }).then(body => {
        cycles += 1;
        // print request ID and scheduled date
        console.log(`${chalk.yellow("=>")} ${chalk.blue(body.data.message)} scheduled ${chalk.magenta(body.data.scheduled)}`);
      })
  }

  res.send(`OK`);
})

app.listen(3000);
node v10.16.0