Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.4 kB
1
Indexable
Never
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const cors = require('cors');
const httpModule = require('http');

// Enable CORS for all origins for HTTP requests
app.use(cors());

app.get('/', (req, res) => {
    res.send('<h1>Hello world</h1>');
});

app.get('/test', (req, res) => {
    // Make an HTTP GET request to test.com
    const options = {
        hostname: 'test.com',
        port: 80,
        path: '/',
        method: 'GET'
    };

    const httpReq = httpModule.request(options, (httpRes) => {
        let data = '';

        httpRes.on('data', (chunk) => {
            data += chunk;
        });

        httpRes.on('end', () => {
            res.send(data); // Send the response from test.com to the client
        });
    });

    httpReq.on('error', (error) => {
        console.error(error);
        res.status(500).send('Error occurred while fetching data from test.com');
    });

    httpReq.end();
});

io.on('connection', (socket) => {
    console.log('a user connected');
    socket.on('disconnect', () => {
        console.log('user disconnected');
    });

    socket.on('/test', ()=>{
        console.log('test connected')
    })
});

server.listen(1234, () => {
    console.log('listening on *:1234');
});
Leave a Comment