Redirection done better, with CloudFlare Workers

A simple HTTP endpoint using CloudFlare Workers

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
var today = new Date();
var dayofthemonth = today.getDate(); //retrieves a numeric value
var month = today.getMonth; //another numeric value, eg 9 for October
var debug = '';
if (dayofthemonth == 17 && today.getMonth() == 11) {
debug='It is 17th of December';
} else {
debug='It is not 17th of December';
}
return new Response('Important message: ' + debug, {status: 200})
}

Date-based re-directs

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
var today = new Date();
var dayofthemonth = today.getDate(); //retrieves a numeric value
var month = today.getMonth; //another numeric value, eg 9 for October
var debug = '';
var before='http://bit.do/ai4ia';
var after='https://github.com/MoBRUEC';
if (dayofthemonth !=22 && today.getMonth() != 5) {
return Response.redirect(before, 302);
} else {
return Response.redirect(after, 302);
}
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url1 = "https://google.com"
const url2 = "https://bing.com"
// If no timezone is needed, just use a plain UTC date
// const date = new Date()
// If a timezone is needed, use toLocaleString and pass a timeZone option
const date = new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
// Coerce the date string with correct timezone back into a date
const hour = new Date(date).getHours()
const url = hour < 12 ? url1 : url2
const response = await fetch(url)
return response
}

Bypassing the Cache

--

--

--

Visit my GitHub readme: https://github.com/MoBRUEC

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

JavaScript console.log( ) Tip🔥 and tricks✨

React Micro Frontend Components in a Monorepo

Part 4: JavaScript Functions → First Class Objects

5 Steps to build a REST API in Node.js with MongoDB

Use Cases and Different Ways to get Files Into Google Cloud Storage

JavaScript Fundamental concept if skip may be killing you

TypeScript With Fewer Types?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mohammed Brückner

Mohammed Brückner

Visit my GitHub readme: https://github.com/MoBRUEC

More from Medium

Reduce Your Energy Bills using MongoDB — Part 2 Getting the Temperature right

Getting Started With Serverless Framework

Organizations should consider purchasing npm libraries

Protestware, Politics, and Open Source Software