MERN博客应用部署DigitalOcean遇ECONNREFUSED错误求助
Hey Tammy, let's tackle your MERN blog deployment issues on DigitalOcean step by step. I'll go through each problem and provide actionable fixes based on the code and details you shared.
http://154.23.54.44:3000访问) Right now, your client is running on port 3000 (likely in development mode with npm start), but in production, you should have your Express server serve the built static files of your React app directly. Here's how to fix this:
1.1 确保客户端已正确构建并由Express服务
- First, on your server, navigate to the project root directory and run
npm run heroku-postbuild— this command installs client dependencies and builds the production version of your React app into theclient/buildfolder. - Then, set the
NODE_ENV=productionenvironment variable and start the Express server:
YourNODE_ENV=production npm startserver.jsis already configured to serve static files fromclient/buildin production mode, so you should now be able to access your React app viahttp://154.23.54.44(port 80, since your server listens onprocess.env.PORT || 80).
1.2 处理端口80的权限问题
- Port 80 is a privileged port, so regular users can't bind to it directly. If you get a permission error when starting the server, you have two options:
- Start the server with
sudo:sudo NODE_ENV=production npm start - Or use
pm2(a process manager) withcap_net_bind_servicepermissions to let non-root users bind privileged ports:
Then start the server with pm2:sudo setcap 'cap_net_bind_service=+ep' $(which node)pm2 start server.js --env production
- Start the server with
1.3 配置Nginx作为反向代理(推荐生产环境做法)
If you don't want Node.js to listen directly on port 80, Nginx is a better choice — it handles static resources, SSL, load balancing, and more. Here's a basic configuration:
- Install Nginx:
sudo apt install nginx - Create a config file:
sudo nano /etc/nginx/sites-available/mern-blog - Paste the following configuration (replace
154.23.54.44with your server IP, and adjust the proxy port to match your Node server's actual port, e.g., 5000):server { listen 80; server_name 154.23.54.44; location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } - Enable the config:
sudo ln -s /etc/nginx/sites-available/mern-blog /etc/nginx/sites-enabled/ - Test the config:
sudo nginx -t - Restart Nginx:
sudo systemctl restart nginx - Make sure your Node server runs on the port specified in the proxy config (update
server.jsto useprocess.env.PORT || 5000if needed) and manage it with pm2.
ECONNREFUSED usually means your server can't reach mLab's database server. Here are the common fixes:
2.1 Check mLab's IP Whitelist
- Log into your mLab console, find your database, go to
Security>IP Whitelist. - Make sure your DigitalOcean server's public IP (
154.23.54.44) is added to the whitelist. You can temporarily add0.0.0.0/0(allow all IPs) for testing, but always switch back to your server's specific IP in production.
2.2 Verify Your mongoURI is Correct
- Check your
configfile'smongoURI— it should follow this format:mongodb://<username>:<password>@dsxxxxxx.mlab.com:xxxxx/your-db-name - Test the connection directly on your server:
If it fails, mLab will return a specific error message to help you pinpoint the issue.mongo <your-mongoURI>
2.3 Check Your Server's Firewall
- DigitalOcean servers use UFW by default. Ensure it allows outbound connections to mLab's port (usually 27017):
sudo ufw out allow 27017/tcp - Verify the firewall rules:
sudo ufw status
- Use pm2 to manage Node processes: pm2 automatically restarts your server if it crashes, and provides logs/monitoring. Install it with
npm install -g pm2, start your server withpm2 start server.js --name mern-blog --env production, save the startup list withpm2 save, and enable auto-start on boot withpm2 startup. - Use environment variables for sensitive data: Don't hardcode
mongoURIor other secrets in config files. Create a.envfile on your server, or set variables withexport MONGO_URI="your-mongo-uri", then fetch them inserver.jswithprocess.env.MONGO_URI. - Fix client API requests: The
proxysetting inclient/package.jsononly works in development. Create a.env.productionfile in your client folder withREACT_APP_API_URL=http://154.23.54.44/api, then useprocess.env.REACT_APP_API_URLas the base URL for your axios requests.
内容的提问来源于stack exchange,提问作者Tammy

