set base URL dynamiically

This commit is contained in:
Dobromir Popov
2024-02-25 13:51:19 +02:00
parent 9baea7c8b7
commit f4e3d9cc2e
2 changed files with 41 additions and 11 deletions

View File

@ -23,6 +23,9 @@ import('get-port').then(module => {
});
process.env.TZ = 'Europe/Sofia';
// Global variable to store the base URL
let baseUrlGlobal;
const PORT = process.env.NEXT_PUBLIC_PORT || 3000;
const HOST = process.env.NEXT_PUBLIC_HOST;
@ -71,6 +74,14 @@ app
// Add the middleware to set 'x-forwarded-host' header
server.use((req, res, next) => {
req.headers['x-forwarded-host'] = req.headers['x-forwarded-host'] || req.headers.host;
// ---------------
if (!baseUrlGloball) {
const protocol = req.headers['x-forwarded-proto'] || 'http';
const host = req.headers.host;
const baseUrl = `${protocol}://${host}`;
baseUrlGlobal = baseUrl;
fs.writeFileSync(path.join(__dirname, 'baseUrl.txt'), baseUrlGlobal, 'utf8');
}
next();
});
server.use("/favicon.ico", express.static("styles/favicon_io/favicon.ico"));
@ -621,4 +632,4 @@ async function Stat() {
Stat();
exports.baseUrlGlobal = baseUrlGlobal;