Last active
May 27, 2016 07:06
-
-
Save ayushgp/7a312ebb9660436f0485135f621bcbdb to your computer and use it in GitHub Desktop.
A cron job that invalidates links and creates new ones.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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