node.js - 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"
}
//生产错误
解决方案
推荐阅读
- ios - 无法发现服务
- hadoop - 子查询在 IMPALA 中有效,但在 HIVE 中无效
- javascript - 使用递增/递减按钮循环回到数字范围的开始或结束
- postgresql - PostgreSQL 10:使用这个 upsert 函数,只有当列值不同时如何更新?
- maven - 特定测试套件的 Maven 运行命令
- java - 在 Springboot 中添加 @EnableWebMvc 会将日期输出从字符串更改为数组
- laravel-5.5 - 邮件发送失败且没有错误
- javascript - 结帐页面上的 BigCommerce 自定义 javascript 不起作用
- javascript - 无法获取按钮的 onclick 属性来调用 JS 函数
- c - 以相反的顺序打印数组指针值