部署到Heroku后,Localhost未连接到mongodb

问题描述 投票:0回答:1

我使用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(`🌎 ==> API server now on port ${PORT}!`);
});

我想知道我的代码是否正在尝试生成连接到mlab所需的生产模式,而不再允许我通过localhost连接到DB?

database mongodb heroku deployment
1个回答
0
投票

实际上,我对来源错了。这是因为我有3001端口在后台运行,所以当我杀死它时解决了问题!

© www.soinside.com 2019 - 2024. All rights reserved.