部署到Heroku后,Localhost未连接到mongodb
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了部署到Heroku后,Localhost未连接到mongodb相关的知识,希望对你有一定的参考价值。
我使用mongodb运行localhost罚款,然后切换到mLab,这也很好,但现在尝试再次从localhost运行数据库,当它尝试连接时出现422错误。
GET http://localhost:3000/api/wines 422 (Unprocessable Entity)
之前我曾经部署过这个网站,并且能够来回切换,所以不确定是什么问题。
这是我的server.js代码:
const express = require("express");
const path = require("path");
const PORT = process.env.PORT || 3001;
const app = express();
const mongoose = require("mongoose");
const routes = require("./routes");
// Connect to the Mongo DB
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/wineDB', { useNewUrlParser: true });
mongoose.connection.on("open", function (ref) {
console.log("Connected to mongo server.");
});
mongoose.connection.on('error', function (err) { console.log(err) });
// require("./models/wine");
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// Add routes, both API and view
app.use(routes);
// Define API routes here
// Send every other request to the React app
// Define any API routes before this runs
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
app.listen(PORT, () => {
console.log(`答案
实际上,我对来源错了。这是因为我有3001端口在后台运行,所以当我杀死它时解决了问题!
以上是关于部署到Heroku后,Localhost未连接到mongodb的主要内容,如果未能解决你的问题,请参考以下文章