// Import Express const express = require('express'); const cors = require('cors'); const http = require('http'); const path = require('path'); const multer = require('multer'); const { Server } = require('socket.io'); const app = express(); let corsOptions = { origin : ['http://localhost:3000'], } app.use(cors(corsOptions)) // Configure Multer for file uploads const upload = multer({ dest: 'uploads/' // Destination folder for uploaded files }); // Define a route app.get('/', (req, res) => { res.send('Welcome to the Express.js'); }); // Start the server app.listen(7777, () => { console.log('HTTP is running on port 7777'); }); app.post('/http', upload.single('file'), (req, res) => { try { if (!req.file) { return res.status(400).send({ message: 'No file uploaded' }); } // Log file information (optional) console.log('File received:', req.file); // Send file back as response res.setHeader('Content-Type', req.file.mimetype); res.sendFile(path.resolve(req.file.path)); } catch (err) { console.error('Error handling file upload:', err); res.status(500).send({ message: 'Error processing file' }); } }); const server = http.createServer(app); const io = new Server(server, { cors: { origin: 'http://localhost:3000', // Adjust frontend URL if necessary methods: ['GET', 'POST'], }, }); io.on('connection', (socket) => { console.log('A user connected:', socket.id); socket.on('sendFile', (data) => { console.log('File received:', data.filename); // Echo the file back to the frontend socket.emit('receiveFile', data); }); socket.on('disconnect', () => { console.log('A user disconnected:', socket.id); }); }); server.listen(8888, () => { console.log('WS listening on port 8888'); });