Skip to content

Instantly share code, notes, and snippets.

@ravisiyer
Created March 30, 2024 09:48
Show Gist options
  • Save ravisiyer/b11699a19c760d883864a729fe8ab9bd to your computer and use it in GitHub Desktop.
Save ravisiyer/b11699a19c760d883864a729fe8ab9bd to your computer and use it in GitHub Desktop.
Barebones blogger blog feed to set of posts (book) using JavaScript and bypassing CORS by using a Node Express proxy server
const express = require("express");
const cors = require("cors");
const { createProxyMiddleware } = require("http-proxy-middleware");
const app = express();
app.use(cors());
app.use(
"/",
createProxyMiddleware({
target: "https://sathyasaiwithstudents.blogspot.com",
changeOrigin: true,
logger: console,
logLevel: "debug",
})
);
app.listen(3001, () => {
console.info("proxy server is running on port 3001");
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<main id="main"></main>
<script src="script.js"></script>
</body>
</html>
// Modified version of https://github.com/hn-88/bloggerToEbook
// Dealing with CORS Errors in JavaScript,
// https://codedamn.com/news/javascript/javascript-cors-error-handling
// suggests proxy server written using Node express as a way to get around CORS
var proxyURL = "http://localhost:3001/"; //Using Node Express local proxy
// Proxy maps requests to https://sathyasaiwithstudents.blogspot.com
// So blog URL need not be passed in fetch request in this program
var forbiddenErrorMsg = `<p>Got 'Forbidden (403)' response.</p>`;
var searchLabel = "3.%20Conversations";
var contentHTML = "";
var mainElement = document.getElementById("main");
async function myFunction() {
var feedReqURL =
proxyURL +
"feeds/posts/default/-/" +
searchLabel +
"?max-results=2&alt=json";
// "feeds/posts/default/" +
// "?max-results=100&alt=json";
var options = {
method: "GET",
muteHttpExceptions: true,
};
var data;
var temp = feedReqURL;
console.log(temp);
try {
response = await fetch(temp, options);
console.log("response");
console.log(response);
if (!response.ok) {
if (response.status === 403) {
mainElement.innerHTML = forbiddenErrorMsg;
} else {
console.log(
"response.ok is false with response.status != 403! Aborting function!"
);
}
return;
}
let data = await response.json();
console.log("data");
console.log(data);
var contentHTML = "";
for (i in data.feed.entry) {
contentHTML +=
"<h1>" +
data.feed.entry[i].title.$t +
"</h1>" +
data.feed.entry[i].content.$t +
'<span style="page-break-after: always" />';
}
// console.log("contentHTML");
// console.log(contentHTML);
mainElement.innerHTML = contentHTML;
} catch (error) {
mainElement.innerHTML = error;
console.log("error");
console.log(error);
}
}
if (mainElement) {
console.log("Calling myFunction");
myFunction();
} else {
alert("main element not found! Aborting!");
}
@ravisiyer
Copy link
Author

This is a HTML/JavaScript modified (and simplified) version of Google Apps Script based https://github.com/hn-88/bloggerToEbook . Key feature of this program is bypassing CORS restriction/error using a Node Express proxy server created using http-proxy-middleware node package. For more on latter, see section "2. Using a proxy server" in https://codedamn.com/news/javascript/javascript-cors-error-handling . To use the program, first the node proxy server has to be run ('node app.js'). Then the index.html file has to be opened in a browser.
Related blog post: Notes on bypassing CORS restriction (error) in JavaScript code that fetches blogger blog feed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment