这是我的server.js
// tools we need
const express = require('express');
const cors = require('cors');
// mongoose helps connect to mongodb database
const mongoose = require('mongoose');
// setting up server
const app = express();
const PORT = process.env.PORT || 5000;
// cors middeware
app.use(cors());
// helps to parse json
app.use(express.json());
const connection = mongoose.connection;
const exercisesRouter = require('./routes/exercises');
const usersRouter = require('./routes/users');
// whenever go to that url, it will load everything in the second parameter
app.use('/exercises', exercisesRouter);
app.use('/users', usersRouter);
if (process.env.NODE_ENV === "production") {
app.use(express.static("./client/build"));
}
//const uri = process.env.ATLAS_URI;
mongoose.connect(process.env.MONGODB_URI || "mongodb://user:password@ds029277.mlab.com:29277/heroku_8npb236z", {
useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true
}
);
// starts listening and starts the server
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}!`);
});
它可以在本地工作并连接到MLab,但是当我部署到Heroku时,它仅显示react前端并且不连接到MLab服务器
这是我在客户端文件夹中的package.json
{
"name": "exercises",
"version": "1.0.0",
"private": true,
"main": "./src/index.js",
"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"
]
},
"devDependencies": {
"nodemon": "^2.0.0"
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.7.9",
"nodemon": "^1.19.4"
},
"proxy": "http://localhost:5000"
}
和我的package.json服务器端
{
"name": "mern-exercise-tracker",
"version": "0.1.0",
"main": "server.js",
"dependencies": {
"axios": "^0.19.0",
"bootstrap": "^4.3.1",
"concurrently": "^5.0.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"if-env": "^1.0.4",
"mongoose": "^5.7.12",
"react": "^16.11.0",
"react-datepicker": "^2.10.0",
"react-dom": "^16.11.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.2.0",
"yarn": "^1.19.1"
},
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "node server.js",
"start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
"client": "cd client && npm run start",
"seed": "node scripts/seedDB.js",
"install": "cd client && yarn install",
"build": "cd client && npm run build",
"heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"
},
"author": "",
"license": "ISC",
"devDependencies": {
"gh-pages": "^2.1.1",
"nodemon": "^2.0.0"
},
"engines": {
"node": "12.13.0"
}
}
为什么会有这个问题?我还有package-lock.json,我不知道它们在做什么。
答案 0 :(得分:0)
在这里您需要输入用户名和密码:
mongodb://user:password@ds029277.mlab.com:29277/heroku_8npb236z
mongodb://test:test123@ds029277.mlab.com:29277/heroku_8npb236z