启动Node.js server.js遇端口占用及Socket.io、Redis连接拒绝问题求助
Hey there, let's tackle your two key issues step by step:
1. Resolving the Port Conflict (8890 taken by Laragon Apache)
You’ve got two straightforward options here to free up or switch ports:
Option A: Change the Node.js Server's Port
The quickest fix is to shift your Express/Socket.io server to an unused port. Just update the server.listen line in your server.js file:
// Replace 8890 with another available port like 8891 server.listen(8891);
Don’t forget to update your frontend’s Socket.io connection URL to match the new port (e.g., io('http://localhost:8891')), otherwise you’ll still hit connection issues.
Option B: Modify Laragon Apache's Port
If you want to keep 8890 for your Node server, adjust Laragon’s Apache settings:
- Open Laragon, click the Menu button > Apache > httpd.conf
- Look for lines like
Listen 8890(or the port Apache is currently using) and change it to something else (e.g.,Listen 8080) - Save the file and restart Apache from the Laragon panel
Verify Port Usage (Optional)
To confirm exactly which process is using 8890, run this command in your terminal:
# Windows netstat -ano | findstr :8890 # macOS/Linux lsof -i :8890
You can then stop the process (via Laragon or task manager) if needed for testing.
2. Fixing the net::ERR_CONNECTION_REFUSED (Socket.io & Redis)
This error typically stems from Redis not running, incorrect connection settings, or mismatched Socket.io ports. Here’s what to check:
Ensure Redis is Running
Laragon often includes Redis—make sure it’s active:
- In Laragon’s panel, look for the Redis icon and click Start if it’s stopped
- If you’re using a standalone Redis instance, confirm the service is running in your OS’s service manager
Correct Redis Connection Configuration
If your Redis instance uses a non-default port, password, or host, update the redis.createClient call in server.js:
// Example with custom settings (adjust to match your Redis setup) var redisClient = redis.createClient({ host: 'localhost', port: 6379, // Default Redis port; change if yours differs password: 'your-redis-password' // Only add this if you’ve set a password });
Add error handling to catch connection issues early:
redisClient.on('error', function(err) { console.error('Redis connection error:', err); });
Double-Check Socket.io Frontend Connection
Make sure your frontend points to the correct Node server port. If you switched to 8891, your frontend code should look like:
const socket = io('http://localhost:8891');
Mismatched ports here will throw the connection refused error every time.
Check Firewall/Security Software
Occasionally, local firewalls or antivirus tools block connections to Node or Redis ports. Temporarily disable them to test, or add exceptions for your Node server and Redis.
Modified server.js Example
Here’s a cleaned-up version of your code with port adjustment and Redis error handling:
var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); var redis = require('redis'); // Use an unused port server.listen(8891); io.on('connection', function (socket) { console.log("client connected"); var redisClient = redis.createClient({ host: 'localhost', port: 6379 }); // Handle Redis connection errors redisClient.on('error', function(err) { console.error('Redis connection issue:', err); }); redisClient.subscribe('message'); // Assuming your subscription was cut off here redisClient.on('message', function(channel, message) { socket.emit(channel, message); }); socket.on('disconnect', function() { redisClient.quit(); }); });
内容的提问来源于stack exchange,提问作者Sava Bežanović

