-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cjs
52 lines (43 loc) · 1.6 KB
/
server.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const express = require('express');
const path = require('path');
const crypto = require('crypto');
const ejs = require('ejs');
const helmet = require('helmet');
const fs = require('fs');
const app = express();
console.log('Starting server...');
console.log(`Environment: ${process.env.NODE_ENV}`);
console.log(`API server: ${process.env.VITE_API_SERVER}`);
console.log(`Organization: ${process.env.ORG_NAME}`);
console.log(`Serving static files from: ${path.join(__dirname, 'dist')}`);
let manifest = {};
const manifestPath = path.join(__dirname, 'dist', '.vite', 'manifest.json');
if (fs.existsSync(manifestPath)) {
manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
console.log('Manifest loaded:', manifest);
} else {
console.log('Manifest not found');
}
app.use((req, res, next) => {
res.locals.nonce = crypto.randomBytes(16).toString('base64');
console.log(`Generated nonce: ${res.locals.nonce}`);
next();
});
const staticPath = path.join(__dirname, 'dist');
app.use(express.static(staticPath));
const templateString = fs.readFileSync(path.join(__dirname, 'template.ejs'), 'utf-8');
const templateFunction = ejs.compile(templateString);
app.get('*', (req, res) => {
const html = templateFunction({
nonce: res.locals.nonce,
title: process.env.ORG_NAME,
cssFile: manifest['src/main.ts']?.css[0] || '',
scriptFilename: manifest['src/main.ts']?.file || '',
apiServer: process.env.VITE_API_SERVER
});
res.send(html);
});
const port = process.env.PORT || 8090;
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});