My Cloudflare Worker is blocking POST requests to my Nodejs app.
POST request created on form submit of https://culturestride[dot]com/universe/recommendmovie
My website architecture:
static html: culturestride[dot]com
nodejs app: culturestride[dot]com/universe/*
jekyll blog: culturestride[dot]com/think/*
I’m using Cloudflare workers to serve subdomain content as a subdirectory according to Subdomain vs. Subdirectory Strategies – Improving SEO Pt 2
When I turn off the Cloudflare worker the posts go through fine.
Can someone help me continue having my subdirectory structure whilst also allowing post requests to go through my worker?
What is the route of your worker?
Maybe, the worker is fetching a worker, and you can’t fetch chaining.
Not quite sure I understand that question sorry!
My worker url is: sparkling-bonus-989d.sohomandarin.workders.dev
Code
// keep track of all our blog endpoints here
const myBlog = {
hostname: “think.culturestride.com”,
targetSubdirectory: “/think”,
assetsPathnames: [“/assets/”]
}
const universe = {
hostname: “universe.culturestride.com”,
targetSubdirectory: “/universe”,
assetsPathnames: [“/static/”]
}
async function handleRequest(request) {
if (request.method === 'GET') {
// returns an empty string or a path if one exists
const formatPath = (url) => {
const pruned = url.pathname.split("/").filter(part => part)
return pruned && pruned.length > 1 ? `${pruned.join("/")}` : ""
}
const parsedUrl = new URL(request.url)
const requestMatches = match => new RegExp(match).test(parsedUrl.pathname)
// if its blog html, get it
if (requestMatches(myBlog.targetSubdirectory)) {
console.log("this is a request for a blog document", parsedUrl.pathname)
const targetPath = formatPath(parsedUrl)
return fetch(`https://${myBlog.hostname}/${targetPath}`)
}
// if its blog assets, get them
if ([myBlog.assetsPathnames].some(requestMatches)) {
console.log("this is a request for blog assets", parsedUrl.pathname)
const assetUrl = request.url.replace(parsedUrl.hostname, myBlog.hostname);
return fetch(assetUrl)
}
// if its universe html, get it
if (requestMatches(universe.targetSubdirectory)) {
console.log("this is a request for a universe", parsedUrl.pathname)
const targetPath = formatPath(parsedUrl)
return fetch(`https://${universe.hostname}/${targetPath}`)
}
// if its blog assets, get them
if ([universe.assetsPathnames].some(requestMatches)) {
console.log("this is a request for universe assets", parsedUrl.pathname)
const assetUrl = request.url.replace(parsedUrl.hostname, universe.hostname);
return fetch(assetUrl)
}
console.log("this is a request to my root domain", parsedUrl.host, parsedUrl.pathname);
// if its not a request blog related stuff, do nothing
return fetch(request)
}
if (request.method === 'POST') {
url_without_query_strings = request.url.split('?')[0] //remove all query strings
const response = await fetch(url_without_query_strings, request);
return response
}
}
I’ve looked at this: Javascript Cloudflare worker script not allowing post requests - Stack Overflow
and gotten it to work only when I modify the line:
const response = await fetch(url_without_query_strings, request);
to an absolute URL:
const response = await fetch('https://culturestride.com/universe/recommendmovie', request);
Update:
Have made it work a couple of times using by changing the code but now I’m hitting 405 method not allowed? Please assist!
if (request.method === 'POST') {
let requestURL = new URL(request.url)
const response = await fetch(requestURL, request);
return response
}