My code is
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.cert')
};
let requestCount = 0;
const server = https.createServer(options, (req, res) => {
requestCount++;
// Process the request here...
console.log("req.url:",req.url);
if (req.url === '/') {
res.end('Hello, world!');
} else {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Custom 404: The requested resource was not found.');
}
});
server.listen(3000, () => {
console.log('Server is running on port 3000');
});
server.on('request', (req, res) => {
console.error('request:', req);
});
server.on('clientError', (error, socket) => {
console.error('Client error:', error);
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.on('error', (error) => {
console.error('Server error:', error);
});
process.on('SIGINT', () => {
console.log('Received SIGINT. Shutting down...');
server.close(() => {
console.log('Server has stopped');
process.exit(0);
});
});
setInterval(() => {
console.log(`Total requests: ${requestCount}`);
}, 5000);`
When I start this server I get message
Server is running on port 3000
Total requests: 0
But when I browse to https://thebomberman.net/game:3000,
I see this text,
The page can’t be found.
It looks like nothing was found at this location.
Total request remains 0. Port 3000 is open and when I run netstat -tulnp,
I see
tcp6 0 0 :::3000 :::* LISTEN 11704/node
Please advise what can be done to fix the 404 error.
I tried catching error logs and used netstat -tulnp to view port 3000 is listening.
>Solution :
The URL you are using, https://thebomberman.net/game:3000, is wrong.
The port should follow the hostname. Try with https://thebomberman.net:3000/game.
You will receive a 404 anyway. You code is repliyng with 200 (Hello world) if the request path is / and 404 for all other paths (like /game).
Moreover, I don’t know if you are using a reverse proxy in front of your node http process. This would change things and I would need to know how the reverse proxy is configured.