https://github.com/e-e-e/cron-router
An express middleware to route requests based on server time.
https://github.com/e-e-e/cron-router
Last synced: 12 days ago
JSON representation
An express middleware to route requests based on server time.
- Host: GitHub
- URL: https://github.com/e-e-e/cron-router
- Owner: e-e-e
- Created: 2016-04-24T02:39:44.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-04-24T02:41:03.000Z (about 10 years ago)
- Last Synced: 2025-10-20T23:47:12.626Z (9 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# cron-router
## About
**Cron-router** is a simple middleware for express which enables routing based on server time. This was initially written to enable a small parasitic art festival to occur on the www.safari.org.au website during the 2016 SafARI festival. The alternative programming hijacked the official website between 9pm to 9am AEST and was different based on the day of the week.
## Usage:
cron-router exposes one function **cronRoute** which takes one argument, and array of Match objects.
Match object should look like this:
```js
var match = {
match: {
year: /* optional, number or {from: number, to: number } */
month: /* optional, number or {from: number, to: number } */
date: /* optional, number or {from: number, to: number } */
day: /* optional number or {from: number, to: number } */
hours: /* optional number or {from: number, to: number } */
minutes: /* optional number or {from: number, to: number } */
},
route: /* an express Router */
}
```
Match properties (year, month, date, etc) accept either an explicit number or a range object {from: x, to: y}. CronRouter compares all the provided match values to the current server date and if equal or within the inclusive range, the request is passed to the provided express router.
### example:
```js
var express = require('express');
var cronrouter = require('cron-router');
var app = express();
var route_A = express.Router();
route_A.get('/',(req,res)=>{
res.send('route A is active between 9pm to midnight on Sundays.');
});)
var route_B = express.Router();
route_B.get('/',(req,res)=>{
res.send('route B is active between midnight to 9am on Mondays.');
});)
var matches = [
{
match: {
day: 0,
hours: {from: 9, to: 24}
},
route: route_A
},{
match: {
day: 1, //sub
hours: {from: 0, to: 8}
},
route: route_B
},
];
app.use(cronrouter.cronRoute(matches));
//if no matches it
app.get('/', (req,res)=> {
res.send('This is the default if none of the cron-routes are matched.');
});
app.listen(8080, (err, suc)=> {
if (err) {
throw err;
} else {
console.log('listening');
}
});
```