I have a virtual machine (VM) running with Google Compute Engine. This website is hosting a Node.JS website using Socket.io. When I connect directly to the website using its IP, it works fine. I then used Google's built-in Cloud DNS to make a zone and connect my VM to my domain.
I assigned my nameservers via my registrar NameCheap and assigned my static VM IP to the zone. I tried to connect to my domain name, but I get the error "Received invalid response". I suspect this is an issue with the port number (80) I'm using, so I change it to port 443, and still nothing.
I suspect this is due to an SSL certificate not being issued to my domain, which I assumed Google would do on my behalf. How should I proceed to fix this bug? I heard about Nginx and Certbot being able to issue SSL certificates, but after 6 hours of having difficulty with them working, I didn't work with them further.
This is the stage I'm currently at, please let me know of anything that could possibly be the issue, and ask for files and more details if needed. I'll attach some relevant files to my project.
server.js
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 fs = require("fs");
async function getCount() {
return new Promise((resolve, reject) => {
fs.readFile("count.json", (err, data) => {
if (err) {
reject(err);
} else {
const count = JSON.parse(data).count;
resolve(count);
}
});
});
}
async function writeCount(count) {
const data = {
count: count
};
return new Promise((resolve, reject) => {
fs.writeFile("count.json", JSON.stringify(data), (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
app.use(express.static("public"));
app.use((req, res, next) => {
res.set('Cache-Control', 'no-store')
next()
})
io.on("connection", (socket) => {
socket.on("error", console.error);
getCount().then(count => {
io.emit("init", count);
}).catch(err => {
console.log(err);
})
socket.on("serversync", (receivedSync) => {
console.log("received " + receivedSync);
if (receivedSync >= 120) {
console.log("not using " + receivedSync);
return;
}
getCount().then(count => {
let total = count + parseInt(receivedSync);
io.emit("clientsync", total);
writeCount(total);
}).catch(err => {
console.log(err);
})
});
});
app.get("/init", (req, res) => {
let files = fs.readdirSync("./public/audio");
res.json([files]);
})
const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}.`);
});
app.yaml
runtime: nodejs
env: flex
manual_scaling:
instances: 1
handlers:
- url: /
static_files: public/index.html
upload: public/index.html
network:
session_affinity: true
forwarded_ports:
- 65080/tcp