-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
103 lines (87 loc) · 3.01 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const express = require('express');
const path = require('path');
const os = require('os');
const multer = require('multer');
const fs = require('fs');
const https = require('https');
const app = express();
const morgan = require('morgan');
// Define the port
const PORT = process.env.PORT || 3000;
// Function to get the local IPv4 address of the wireless LAN
function getLocalIPAddress() {
const interfaces = os.networkInterfaces();
for (let iface in interfaces) {
if (iface.includes('Wi-Fi') || iface.includes('Wireless')) {
for (let alias of interfaces[iface]) {
if (alias.family === 'IPv4' && !alias.internal) {
return alias.address;
}
}
}
}
return 'localhost';
}
// Serve static files from the /public directory
app.use(express.static(path.join(__dirname, 'public')));
// Path to the access log file
const logFilePath = path.join(__dirname, 'access.log');
// Check if the access log file exists, create it if it doesn't
if (!fs.existsSync(logFilePath)) {
fs.writeFileSync(logFilePath, '', { flag: 'w' });
}
// Create a write stream (in append mode) to log file
const accessLogStream = fs.createWriteStream(logFilePath, { flags: 'a' });
// Setup the logger
app.use(morgan('combined', { stream: accessLogStream }));
// Custom rule: Log each request
app.use((req, res, next) => {
console.log(`Request from ${req.ip} for ${req.url}`);
next();
});
// Custom rule: Log each request
app.use((req, res, next) => {
console.log(`${req.method} request for '${req.url}'`);
next();
});
// Set up multer for file uploads, keeping the original filename
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'shared/');
},
filename: (req, file, cb) => {
cb(null, file.originalname);
}
});
const upload = multer({ storage: storage });
app.use(express.static('public'));
// Handle file uploads
app.post('/upload', upload.single('file'), (req, res) => {
if (!req.file) {
return res.status(400).send('No files were uploaded.');
}
else {
const filePath = `../shared/${req.file.originalname}`;
res.send(`<p>File uploaded successfully. <a href="${filePath}">Download ${req.file.originalname}</a></p>`);
}
});
// Handle file downloads
app.get('/download', (req, res) => {
const filename = req.query.filename;
const filePath = path.join(__dirname, 'shared', filename);
if (fs.existsSync(filePath)) {
res.download(filePath);
} else {
res.status(404).send('File not found');
}
});
app.use(express.static('public'));
// SSL options
const sslOptions = {
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert.pem'))
};
// Create HTTPS server
https.createServer(sslOptions, app).listen(PORT, () => {
console.log(`Server is running at http://${getLocalIPAddress()}:${PORT}`);
});