Spaces:
Runtime error
Runtime error
Create app.js
Browse files
app.js
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const os = require('os');
|
3 |
+
|
4 |
+
const app = express();
|
5 |
+
|
6 |
+
app.get('/', (req, res) => {
|
7 |
+
res.send('Hello, Express.js!');
|
8 |
+
});
|
9 |
+
|
10 |
+
app.get('/about', (req, res) => {
|
11 |
+
res.send('About page');
|
12 |
+
});
|
13 |
+
|
14 |
+
app.get('/users/:userId', (req, res) => {
|
15 |
+
const userId = req.params.userId;
|
16 |
+
res.send(`User ID: ${userId}`);
|
17 |
+
});
|
18 |
+
|
19 |
+
const PORT = process.env.PORT || 3000;
|
20 |
+
|
21 |
+
// Get local IP address
|
22 |
+
const networkInterfaces = os.networkInterfaces();
|
23 |
+
let localIpAddress = '';
|
24 |
+
|
25 |
+
Object.keys(networkInterfaces).forEach(interfaceName => {
|
26 |
+
networkInterfaces[interfaceName].forEach(network => {
|
27 |
+
if (network.family === 'IPv4' && !network.internal) {
|
28 |
+
localIpAddress = network.address;
|
29 |
+
}
|
30 |
+
});
|
31 |
+
});
|
32 |
+
|
33 |
+
// If no local IP address found, fallback to localhost
|
34 |
+
if (!localIpAddress) {
|
35 |
+
localIpAddress = 'localhost';
|
36 |
+
}
|
37 |
+
|
38 |
+
app.listen(PORT, localIpAddress, () => {
|
39 |
+
console.log(`Server is running on http://${localIpAddress}:${PORT}`);
|
40 |
+
});
|