首页 > 解决方案 > MERN App在digitalocean中出现连接错误

问题描述

我已经创建了我的Blog使用MERN应用程序。所以现在我将它托管在digitalocean服务器中,但ECONNREFUSED出现错误。我已经关注了 youtube 和其他文件中的大部分文件来托管它。

我可以看到我的客户端正在运行,ex: http://154.23.54.44:3000但它没有运行:3000,也没有连接我的mlab连接。

任何解决此问题并在实际生产中运行此应用程序的建议非常感谢。

//服务器包.json

"main": "server.js",
  "scripts": {
    "client-install": "npm install --prefix client",
    "start": "node server.js",
    "server": "nodemon server.js",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run server\" \" npm run client\"",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
  }

//server.js

const express = require('express');
const mongoose = require('mongoose');

const path = require('path');
const config =  require('config');

const app = express();

//bodyParser middleware
app.use(express.json());

//DB config
const db = config.get('mongoURI');

//connect to mongo
mongoose
.connect(db, {useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true})
.then(() => console.log('MongoDB connected...'))
.catch(err => console.log(err));

//use routes
app.use('/api/users', require('./routes/api/users'));
app.use('/api/auth', require('./routes/api/auth'));


// Serve static assets if in production
if(process.env.NODE_ENV === 'production') {
    // SET static folder
    app.use(express.static('client/build'));
    app.get('*', (req, res) => {
        res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
    });
}

const port = process.env.PORT || 80;

app.listen(port, () => console.log(`Server started on PORT ${port}`));

//客户端(package.json)

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "axios": "^0.19.2",
    "bootstrap": "^4.4.1",
    "bootstrap-less": "^3.3.8",
    "moment": "^2.24.0",
    "node-sass": "^4.13.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "proxy": "http://localhost:5000"
}

//生产错误

在此处输入图像描述

标签: node.jsreactjsexpressmongoosedigital-ocean

解决方案


推荐阅读