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,27 @@
|
|||||||
|
FROM node:18
|
||||||
|
WORKDIR /config
|
||||||
|
|
||||||
|
ENV API_URL https://api.comedykit.be/
|
||||||
|
ENV ROOT_PATH /react-app
|
||||||
|
ENV DEFAULT_TITLE ComedyKit
|
||||||
|
env DEFAULT_DESCRIPTION Ontdek je lach en deel je humor op het meest bruisende comedy platform van Vlaanderen.
|
||||||
|
|
||||||
|
# Create app directory
|
||||||
|
WORKDIR /server
|
||||||
|
|
||||||
|
# Install app dependencies
|
||||||
|
# A wildcard is used to ensure both package.json AND package-lock.json are copied
|
||||||
|
# where available (npm@5+)
|
||||||
|
COPY package*.json ./
|
||||||
|
|
||||||
|
RUN npm install pm2 -g
|
||||||
|
RUN npm install
|
||||||
|
# If you are building your code for production
|
||||||
|
# RUN npm ci --omit=dev
|
||||||
|
|
||||||
|
# Bundle app source
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN pm2-runtime "node index.js"
|
||||||
|
|
||||||
|
EXPOSE 3000
|
||||||
@@ -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...");
|
||||||
|
});
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "metatag-server",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Add metatags to the build app",
|
||||||
|
"main": "index.js",
|
||||||
|
"author": "Bram Kelchtermans",
|
||||||
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"start": "env-cmd -f /config/.env node index.js",
|
||||||
|
"start:dev": "env-cmd -f ./environments/.dev.server.env node index.js",
|
||||||
|
"start:prod": "env-cmd -f ./environments/.prod.server.env node index.js",
|
||||||
|
"start:stage": "env-cmd -f ./environments/.stage.server.env node index.js"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"axios": "^1.4.0",
|
||||||
|
"buffer": "^6.0.3",
|
||||||
|
"env-cmd": "^10.1.0",
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"fs": "^0.0.1-security",
|
||||||
|
"path": "^0.12.7"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
1.0
|
||||||
Reference in New Issue
Block a user