You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

问题1:无法通过不带端口的IP访问应用(只能用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 the client/build folder.
  • Then, set the NODE_ENV=production environment variable and start the Express server:
    NODE_ENV=production npm start
    
    Your server.js is already configured to serve static files from client/build in production mode, so you should now be able to access your React app via http://154.23.54.44 (port 80, since your server listens on process.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) with cap_net_bind_service permissions to let non-root users bind privileged ports:
      sudo setcap 'cap_net_bind_service=+ep' $(which node)
      
      Then start the server with pm2:
      pm2 start server.js --env production
      

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.44 with 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.js to use process.env.PORT || 5000 if needed) and manage it with pm2.
问题2:无法连接mLab数据库(ECONNREFUSED错误)

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 add 0.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 config file's mongoURI — it should follow this format:
    mongodb://<username>:<password>@dsxxxxxx.mlab.com:xxxxx/your-db-name
    
  • Test the connection directly on your server:
    mongo <your-mongoURI>
    
    If it fails, mLab will return a specific error message to help you pinpoint the issue.

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 with pm2 start server.js --name mern-blog --env production, save the startup list with pm2 save, and enable auto-start on boot with pm2 startup.
  • Use environment variables for sensitive data: Don't hardcode mongoURI or other secrets in config files. Create a .env file on your server, or set variables with export MONGO_URI="your-mongo-uri", then fetch them in server.js with process.env.MONGO_URI.
  • Fix client API requests: The proxy setting in client/package.json only works in development. Create a .env.production file in your client folder with REACT_APP_API_URL=http://154.23.54.44/api, then use process.env.REACT_APP_API_URL as the base URL for your axios requests.

内容的提问来源于stack exchange,提问作者Tammy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:57:51