Skip to content

Instantly share code, notes, and snippets.

@ayushgp
Last active May 27, 2016 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayushgp/7a312ebb9660436f0485135f621bcbdb to your computer and use it in GitHub Desktop.
Save ayushgp/7a312ebb9660436f0485135f621bcbdb to your computer and use it in GitHub Desktop.
A cron job that invalidates links and creates new ones.
var express = require("express");
var app = express();
function randomString(length) {
var result = '';
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for (var i = length; i > 0; --i)
result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
var cron = require('node-cron');
var rString = randomString(12);
cron.schedule('15,30,45 * * * * *', function(){
rString = randomString(12);
console.log("Your random route is: localhost:3000/"+rString);
});
console.log("Your random route is: localhost:3000/"+rString);
app.get("/:randomStr", function(req, res){
if(req.params.randomStr === rString)
res.send("Yo!")
else
res.send("Sorry no link found!");
});
app.get("*",function(req,res){
res.send("Sorry no link found!");
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment