chore: Update Dockerfile to include Node.js version 18 and install pm2 globally
Build and Push Docker Images / build-and-push (push) Has been cancelled
Build and Push Docker Images / build-and-push (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const path = require('path');
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const fs = require("fs");
|
||||
const { default: axios } = require('axios');
|
||||
const { btoa } = require('buffer');
|
||||
//const indexPath = path.resolve(__dirname, '..', 'build', 'index.html');
|
||||
const indexPath = path.resolve(process.env.ROOT_PATH, 'index.html');
|
||||
const iconPath = path.resolve(process.env.ROOT_PATH, 'favicon.ico');
|
||||
const staticPath = path.resolve(process.env.ROOT_PATH, "static");
|
||||
|
||||
app.enable('trust proxy');
|
||||
|
||||
// static resources should just be served as they are
|
||||
app.use("/static", express.static(
|
||||
staticPath,
|
||||
{ maxAge: '30d' },
|
||||
));
|
||||
|
||||
const fetchMetaTags = async (url) => {
|
||||
const encoded = btoa(url);
|
||||
const resp = await axios.get(`${process.env.API_URL}meta?path=${encoded}`);
|
||||
console.log(resp.data)
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
const fillMetaTags = async (request, htmlData) => {
|
||||
const url = request.originalUrl;
|
||||
|
||||
let title = process.env.DEFAULT_TITLE;
|
||||
let description = process.env.DEFAULT_DESCRIPTION;
|
||||
let image = "";
|
||||
let icon = "/favicon.ico"
|
||||
|
||||
try {
|
||||
const resp = await fetchMetaTags(url);
|
||||
title = resp.title ? resp.title : title;
|
||||
description = resp.description ? resp.description : description;
|
||||
image = resp.image ? resp.image : image;
|
||||
icon = resp.icon ? resp.icon : icon;
|
||||
} catch (e) {
|
||||
console.error(e.message)
|
||||
console.log("ERROR FETCHING TAGS");
|
||||
}
|
||||
|
||||
htmlData = htmlData.replace(
|
||||
"<title></title>",
|
||||
`<title>${title}</title>`
|
||||
)
|
||||
.replace(/__META_FAVICON__/g, icon)
|
||||
.replace(/__META_OG_TITLE__/g, title)
|
||||
.replace(/__META_OG_DESCRIPTION__/g, description)
|
||||
.replace(/__META_DESCRIPTION__/g, description)
|
||||
.replace(/__META_OG_IMAGE__/g, image)
|
||||
|
||||
//console.log(htmlData);
|
||||
return htmlData;
|
||||
|
||||
}
|
||||
|
||||
app.get('/favicon.ico', (req, res, next) => {
|
||||
return res.sendFile(iconPath)
|
||||
})
|
||||
|
||||
app.get('/*', (req, res, next) => {
|
||||
fs.readFile(indexPath, 'utf8', async (err, htmlData) => {
|
||||
if (err) {
|
||||
console.error('Error during file reading', err);
|
||||
return res.status(404).end()
|
||||
}
|
||||
// inject meta tags
|
||||
htmlData = await fillMetaTags(req, htmlData);
|
||||
return res.send(htmlData);
|
||||
});
|
||||
});
|
||||
app.listen(PORT, "0.0.0.0", (error) => {
|
||||
if (error) {
|
||||
return console.log('Error during app startup', error);
|
||||
}
|
||||
console.log("Listening for requests...");
|
||||
});
|
||||
Reference in New Issue
Block a user