-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (64 loc) · 2.14 KB
/
server.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
65
66
67
68
69
70
71
72
73
74
// Import necessary modules and dependencies
const express = require('express');
const { config } = require('dotenv');
const path = require('path');
const fs = require('fs');
const { log } = require('./utilities/logger');
// Load environment variables from .env file
config();
// Create an Express application
const app = express();
// Set the root directory for HTML files
const publicPath = path.join(__dirname, 'public');
app.use(express.static(publicPath));
// Define error handling middleware
app.use((err, req, res, next) => {
// Log the error
log(`Unhandled error: ${err.message}`, 'error');
// Send an error response to the client
res.status(500).send('Internal Server Error');
});
// Define middleware to handle requests for non-existent routes
app.use((req, res, next) => {
// Log the request for non-existent routes
log(`404 Not Found - ${req.method} ${req.url}`, 'warn');
// Send a 404 response to the client
res.status(404).send('404 Not Found');
});
// Define the port number from environment variables or use a default value
const PORT = process.env.PORT || 3000;
// Start the server
app.listen(PORT, () => {
log(`Server is running on port ${PORT}`, 'info');
});
// Automatically serve HTML files with specific routes
const serveHTMLFiles = () => {
fs.readdir(publicPath, (err, files) => {
if (err) {
log(`Error reading directory: ${err.message}`, 'error');
return;
}
files.forEach((file) => {
if (file.endsWith('.html')) {
let route = `/${file.slice(0, -5)}`;
if (file === 'index.html') {
route = '/';
}
app.get(route, (req, res) => {
const filePath = path.join(publicPath, file);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
log(`Error reading file ${file}: ${err.message}`, 'error');
res.status(500).send('Internal Server Error');
return;
}
res.send(data);
log(`Served ${file} automatically at route ${route}`, 'info');
});
});
log(`Route ${route} added for ${file}`, 'info');
}
});
});
};
serveHTMLFiles();