Express Node.js服务器无法加载game.html,提示安全连接错误
Hey there, let’s break down why your game.html isn’t loading and fix this step by step:
1. First, check your button’s link format
Take a look at the button code in public/index.html—did you accidentally set the link to https://localhost:3000/game.html? If your server is running on plain HTTP (the default for local dev), forcing HTTPS will trigger that "invalid response" error because localhost doesn’t have a valid SSL certificate by default.
Use a relative path or explicit HTTP path instead:
<!-- Relative path is best for flexibility --> <button onclick="window.location.href='game.html'">Go to Game Page</button> <!-- Or explicitly use HTTP --> <button onclick="window.location.href='http://localhost:3000/game.html'">Go to Game Page</button>
2. Make sure your server serves all static files
Open up server/server.js—if you’re only handling the root (/) route and sending index.html directly, your server won’t know how to respond to requests for /game.html.
For example, this is a common mistake (only serves index.html at root):
app.get('/', (req, res) => { res.sendFile(path.join(__dirname, '../public/index.html')); });
Fix it by using the static file middleware (assuming you’re using Express) to host the entire public directory:
const express = require('express'); const path = require('path'); const app = express(); // Serve all files in the public folder app.use(express.static(path.join(__dirname, '../public'))); app.listen(3000, () => { console.log('Server running at http://localhost:3000'); });
This way, any request for files in public (like game.html) will be handled automatically.
3. Check if your server is accidentally using HTTPS
If your server.js uses the https module with self-signed (or missing) SSL certificates, that’ll cause the secure connection error too. Look for code like this:
const https = require('https'); const fs = require('fs'); const sslOptions = { key: fs.readFileSync('some-key.pem'), cert: fs.readFileSync('some-cert.pem') }; https.createServer(sslOptions, app).listen(3000);
If this is the case, either switch back to HTTP (use the http module instead) or generate a valid local SSL certificate (tools like mkcert work great for this).
4. Clear browser cache and restart your server
Sometimes old cached redirects or a hanging server process can cause weird issues. Try:
- Closing all browser tabs and reopening
http://localhost:3000 - Stopping your server (Ctrl+C in the terminal) and restarting it with
node server/server.js
Go through these steps, and your game.html should load without issues!
内容的提问来源于stack exchange,提问作者rohan

