Shutdown server at certain time

lets say i have a demo, and to keep people from abusing it i want the server to shutdown at 5pm GMT. How can i achieve this with JS?

Prefer to use a button on a website

You could use the standard process.exit() server side to shutdown a project in response to a request at specific end point that your button connects to. But Glitch will restart your project on the next HTTP request so that doesn’t really help you in this case.

I’d probably make it so the button connects to an endpoint that flips a Boolean that prevents all future requests from completing. Or if you wanted to do it without the button you could just set a cron job to flip that variable at a desired time.

1 Like

if youre using express you could lead / to a page where it says site is shutdown

1 Like

You can use a setInterval to check the time, if the time is certain hours in the day you can simple close the http server. I.e.

let closed = false;

setInterval(() => {
  let h = new Date().getHours();
  if (!closed && h >= 17 && h < 24) {
    server.close();
    closed = true;
  }
  if (closed && h < 17) {
    server.listen(3000);
    closed = false;
  }
}, 30000)

Anyhow the server is still most likely to shutdown due to inactivity, therefore started if someone tries to access the page again.

1 Like

Here’s an example that should help.

The first codeblock (should-be-active.js) is a module which exports a function that you can use in your server script:

should-be-active.js:

// add leading zero to string time values (e.g. 3 -> 03)
const padTimeValue = n => String(n).padStart(2, '0');

// convert time string to decimal hours (e.g. 17:15 -> 17.25)
const getDecimalHours = timeString => {
  const [hours, minutes] = timeString.split(':').map(nString => parseInt(nString, 10));
  return hours + (minutes / 60);
};

// get current time in HH:mm string format
const getNowTime = () => {
  const date = new Date();
  const hours = date.getUTCHours();
  const minutes = date.getUTCMinutes();
  return `${padTimeValue(hours)}:${padTimeValue(minutes)}`;
};

// the current time is between startTime and endTime (active hours)
const shouldBeActive = (startTime, endTime) => {
  const nowTime = getNowTime();
  return (
    getDecimalHours(nowTime) >= getDecimalHours(startTime)
    && getDecimalHours(nowTime) <= getDecimalHours(endTime)
  );
};

module.exports = {
  shouldBeActive,
};

Here’s how you’d use it in your server script:

server.js:

const {shouldBeActive} = require('./should-be-active.js');

// set these to your app's daily active hours
const startTime = '00:00';
const endTime = '17:00';

// check if outside active hours
if (!shouldBeActive(startTime, endTime)) {
  // any functionality that should happen outside active hours (e.g. respond with a 404, etc.)
  console.log('App outside active hours');
  // finally, terminate the node process so no further code executes
  process.exit();
}

// the rest of your app code here
console.log('App is active');
2 Likes

Thank you for your help!

1 Like