Trigger An API Call When The Server Restarts

I needed to extend express to send an API request to an internal endpoint whenever the application starts to reattach workers to bull queues. It doesn’t depend on nodemon events etc.

You can use it by creating a file myfile.js in /extensions/server_connect/routes

Use dotenv if you need API tokens or other credentials.

const request = require("request");
let requestSent = false;

exports.handler = function (app) {
  if (requestSent) return;
  requestSent = true;

  const options = {
    method: "POST",
    url: "myapp.url",
    headers: {
      Authorization: "Bearer YOUR_TOKEN",
    },
  };
  request(options, (error, response, body) => {
    if (error) throw new Error(error);
    console.log(body);
  });
};

Community Page
Last updated: