我一直在搜索,无法找到解决我问题的方法。
我是MERN堆栈的新手,主要从事前端工作。
该项目是使用MongoDB ATLAS,React,Express和node.js创建的
我已将应用程序部署到heroku,并且正在显示该应用程序,但我无法使用它,但出现错误Failed to load resource: net::ERR_CONNECTION_REFUSED
。但是我可以看到它的前端并与之交互,但是我认为后端无法正常工作。我可以浏览页面等。但是当我输入信息并尝试将其存储到服务器时,什么也没发生,只是前端部分,但没有任何连接或发送到服务器,我一直收到该连接被拒绝的错误消息。
然后,此错误紧接着出现:Uncaught (in promise) Error: Network Error
我正在使用mongoDB地图集,我不确定这是否有所作为。我确实相信我会将其设置为也可以在任何地方访问,而不仅仅是我的IP。
现在,当我在本地计算机上运行时,一切正常。如果我使用的是Heroku部署版本,并且在运行我的本地计算机服务器的情况下工作正常。但是,一旦我关闭本地服务器,该错误就会重新出现在heroku部署的版本上。
这是我的server.js文件:
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
/* for heroku deployment */
const path = require('path');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
/* for heroku deployment */
app.use(express.static(path.join(__dirname, "client", "build")));
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true });
const connection = mongoose.connection;
connection.once('open', () => {
console.log("MongoDB database connection established successfully")
})
const exerciseRouter = require('./routes/exercises');
const userRouter = require('./routes/users');
app.use('/exercises', exerciseRouter);
app.use('/users', userRouter);
/* for heroku deployment */
if(process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
})
}
app.listen(port, () => {
console.log(`Server is running on port: ${port}`)
});
我也尝试过:
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
})
而不是有条件的。
这是我的后端 package.json文件。
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"engines": {
"node": "10.15.3"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.7.12"
}
}
我也尝试过:
"heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"
在这里。
这是我的前端 package.json文件:
{
"name": "mern-stack-project",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"react": "^16.12.0",
"react-datepicker": "^2.10.1",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:5000",
"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"
]
}
}
如果您想亲自查看,这是指向heroku上部署的站点的链接:https://full-stack-exercise-tracker.herokuapp.com/
如果您需要我提供更多代码文件,请询问。就像我说的那样,这是我的第一个MERN堆栈应用程序和MERN堆栈部署。
感谢您的帮助,谢谢!