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
| const express = require('express'); const multiparty = require('multiparty'); const fs = require('fs'); const path = require('path'); const os = require('os');
const app = express();
app.post('/', (req, res) => { const form = new multiparty.Form(); form.uploadDir = generateSavePath(req); console.log(form.uploadDir) if (!fs.existsSync(form.uploadDir)) { fs.mkdirSync(form.uploadDir, {recursive: true}); } form.keepExtensions = true; form.parse(req, (err, fields, files) => { if (err) { console.error('Error parsing form:', err); return res.status(500).json({error: 'Error parsing form'}); }
console.log('Fields:', fields); console.log('Files:', files);
res.json({message: 'File uploaded successfully.'}); }); }); const generateSavePath = (req) => { const vpnStart = req.headers['vpn_start_time']; const appName = req.headers['app_name']; const ipAndPort = req.headers['ipandport']; return path.join(__dirname, 'uploads', vpnStart, appName, ipAndPort); };
function getLocalIP() { const interfaces = os.networkInterfaces(); for (const name of Object.keys(interfaces)) { for (const net of interfaces[name]) { if (net.family === 'IPv4' && !net.internal) { return net.address; } } } return '0.0.0.0'; }
app.listen(3000, () => { const localIP = getLocalIP(); console.log(`Server is running on http://${localIP}:3000`); });
|