1

i having an issue while using node.js with apache on site.

if using http without SSL node.js with apache are working with my domain at env. file If I remove the SSL code it runs fine, however with it I get a request to http://mydomain.io/socket.io/1/?t=XXXXXXXXX

but when i enable SSL with let encrypt

my site are working but connect with Node js + socket io having error 404

*Note it's not trying https, which causes it to fail.

I'm testing on chrome, firefox and edge still can't fix.

I apologize if this is a simple question, I'm a node/socket.io newbie.

Thanks!


Here my details code are working when using http | but not working using https with let encrypt domain

const pino = require('pino')
const https = require('https');
const { Boom } = require('@hapi/boom')
const fs = require('fs')
const chalk = require('chalk')
require('dotenv/config')
const express = require('express')
const socket = require("socket.io");
const { toDataURL } = require('qrcode')
const mysql = require('mysql');
require('dotenv').config();
const request = require('request');

const app = express()
const host = process.env.HOST ?? '127.0.0.1'
const port = parseInt(process.env.PORT ?? 3000)
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
const ser = app.listen(port, host, () => {
    console.log(`Server is listening on http://${host}:${port}`)
})
const io = socket(ser);

const db = mysql.createConnection({
    host: process.env.DB_HOSTNAME,
    user: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE
});

db.connect((err) => {
    if (err) throw err;
    console.log('Mysql Connected...');
});

const sessionMap = new Map()

1 Answer 1

1

I've had a similar issue before, you need 2 Different servers, one for http, and one for https.

var usinghttps = false;
if(usinghttps) {
    var options = {
        key: fs.readFileSync("/etc/letsencrypt/live/us2.swordbattle.io/privkey.pem"),
        cert: fs.readFileSync("/etc/letsencrypt/live/us2.swordbattle.io/fullchain.pem"),
    };
 httpsserver = https.createServer(options, app).listen(443);
}
 server = http.createServer(app); 

And then to create the socket.io server,

const io = new Server(usinghttps ? httpsserver:server);

This personally worked for me, not sure if it works on all apps.

Sign up to request clarification or add additional context in comments.

5 Comments

hi, 2 different server mean 2 vps ? or else ?
Not 2 vps, one vps running 2 servers, one server listening on http:// and one listening on https://
You can run 2 server with one script using code above
so if use 1 server vps = 2 connection http & https also can right ?
yes, that is possible, that's what im currently doing with my game swordbattle.io

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.