-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (52 loc) · 1.88 KB
/
app.js
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
53
54
55
56
57
58
59
60
61
62
63
64
/* /Users/atgehrhardt/Dev/TableTap/app.js */
const express = require('express');
const fs = require('fs');
const path = require('path');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const app = express();
// Serve static files from the 'public' directory
app.use(express.static('public'));
// Load Service Worker
app.get('/service-worker.js', (req, res) => {
res.sendFile(path.resolve(__dirname, 'service-worker.js'));
});
// Set up EJS as the view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'public', 'views'));
// Read the images from the 'boards' directory
const boardsDirectory = path.join(__dirname, 'public', 'boards');
const getBoardImages = async (dir = boardsDirectory, prefix = '') => {
let boardImages = [];
const files = fs.readdirSync(dir);
for (let file of files) {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
boardImages = [...boardImages, ...await getBoardImages(filePath, `${prefix}${file}/`)];
} else {
const extension = path.extname(file).toLowerCase();
if (extension === '.jpg' || extension === '.png' || extension === '.svg') {
const modalFilePath = path.join(dir, 'modal.json');
let modalContent = {};
try {
modalContent = JSON.parse(await readFile(modalFilePath, 'utf-8'));
} catch (err) {
console.error(err);
}
boardImages.push({ image: `${prefix}${file.slice(file.lastIndexOf('/') + 1)}`, modalContent });
}
}
}
return boardImages;
};
// Render the index page with the board images
app.get('/', async (req, res) => {
const boardImages = await getBoardImages();
res.render('index', { activeView: 'boards', boardImages });
});
// Start the server
const port = 7777;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});