我正在尝试使用node.js,react和MongoDB将我的应用程序部署到Heroku;部署成功;但是,该网站无法从MongoDB获取数据。
错误是GET http://localhost:5000/comment net::ERR_CONNECTION_REFUSED
和Uncaught (in promise) Error: Network Error
,我认为这是因为后端没有运行node server.js
。谁能帮助我正确部署此方法?
我在后端的pakage.json是
"client-install": "npm install --prefix myprofile","start":"node server.js","server":"nodemon server.js","client":"npm start --prefix myprofile","dev":"concurrently \"npm run server\" \"npm run client\"","heroku-postbuild":"NPM_CONFIG_PRODUCTION=false npm install --prefix myprofile && npm run build --prefix myprofile"
}
我的server.js是
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const path = require("path");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;
app.use(express.json());
app.use(cors());
//app.use(express.static(path.join(__dirname,'./myprofile/build')));
const uri = process.env.ATLAS_URI;
mongoose.connect(uri,{
useNewUrlParser: true,useCreateIndex: true,useUnifiedTopology: true
});
const connection = mongoose.connection;
connection.once("open",() => {
console.log("MongoDB data connected");
});
const CommentRouter = require("./CommentRouter");
app.use("/comment",CommentRouter);
if (process.env.NODE_ENV === "production") {
app.use(express.static("myprofile/build"));
app.get("*",(req,res) => {
res.sendFile(path.resolve(__dirname,"myprofile","build","index.html"));
});
}
app.listen(port,() => console.log(`Server is running on ${port}`));
非常感谢您!