-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
78 lines (68 loc) · 2.29 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
75
76
77
78
const express = require('express');
const path = require('path');
const fs = require('fs');
const { marked } = require('marked');
const net = require('net');
const os = require('os');
const app = express();
const folderName = '.';
const readmePath = path.join(__dirname, folderName, 'README.md');
// Serve static files under /lsm route
app.use('/lsm', express.static(path.join(__dirname, folderName)));
// Serve README.md at /lsm as the default content
app.get('/lsm', (req, res) => {
fs.readFile(readmePath, 'utf8', (err, data) => {
if (err) {
res.status(500).send('Error reading README.md');
} else {
const htmlContent = marked(data);
res.send(htmlContent);
}
});
});
// Redirect root (/) to /lsm/
app.get('/', (req, res) => {
res.redirect('/lsm/');
});
// Handle 404 errors for undefined routes
app.use((req, res) => {
res.status(404).send('404: Not Found');
});
// Function to get the local IP address
const getLocalIPAddress = () => {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return 'localhost'; // Fallback in case no IP is found
};
// Function to check if a port is available
const checkPort = (port) => {
return new Promise((resolve, reject) => {
const tester = net
.createServer()
.once('error', (err) => (err.code === 'EADDRINUSE' ? resolve(false) : reject(err)))
.once('listening', () => tester.once('close', () => resolve(true)).close())
.listen(port);
});
};
// Start the server with dynamic port allocation
const startServer = async (port) => {
let currentPort = port;
while (!(await checkPort(currentPort))) {
console.log(`Port ${currentPort} is taken. Trying port ${currentPort + 1}...`);
currentPort++;
}
const ipAddress = getLocalIPAddress();
app.listen(currentPort, () => {
console.log(`Server is running at http://${ipAddress}:${currentPort}/lsm/`);
});
};
// Start the process by checking port 3000
startServer(3000).catch((err) => {
console.error('Error starting the server:', err);
});