-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (36 loc) · 1.21 KB
/
index.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
const dotenv = require('dotenv').config();
const express = require('express');
const app = express();
const path = require('path');
const cors = require('cors');
const healthRouter = require("./routes/health.route");
const dirRouter = require("./routes/dir.route");
const { logger, requestTimingLogger } = require('./logger/logger');
app.use(cors());
app.use(express.json());
app.use(logger);
app.get("/", (req,res)=>{
const htmlFilePath = path.join(__dirname, 'index.html');
res.sendFile(htmlFilePath, (err) => {
if (err) {
console.error('Error sending index file:', err);
res.status(500).json({
error: 'Failed to load health page',
});
}
});
});
app.use('/cdn', express.static(path.join(__dirname, process.env.FILE_DIRECTORY)));
app.get('/cdn', (req, res) => {
res.setHeader('Cache-Control', 'public, max-age=3600');
res.sendFile(path.join(__dirname, process.env.FILE_DIRECTORY, req.params[0]));
});
app.use("/dir",dirRouter);
app.use("/health",healthRouter);
app.use((req, res) => {
res.status(404).json({ error: 'Resource not found', path: req.originalUrl });
});
const PORT = process.env.PORT;
app.listen(PORT, () => {
console.log(`Server running on PORT ${PORT}`);
});