我创建了一个使用mongoDB Atlas的MERN应用。这是我第一次将应用程序部署到Heroku,并且遇到以下错误:
2019-10-03T00:04:49+00:00 app[api]: Build succeeded
2019-10-03T00:05:06.483971+00:00 heroku[web.1]: Starting process with command `npm start`
2019-10-03T00:05:09.194221+00:00 app[web.1]:
2019-10-03T00:05:09.194239+00:00 app[web.1]: > trajectory@0.1.0 start /app
2019-10-03T00:05:09.194241+00:00 app[web.1]: > node server.js
2019-10-03T00:05:09.194243+00:00 app[web.1]:
2019-10-03T00:05:10.386062+00:00 app[web.1]: Server started on port 44482
2019-10-03T00:05:10.439527+00:00 app[web.1]: (node:23) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
2019-10-03T00:05:10.587698+00:00 app[web.1]: { MongoNetworkError: failed to connect to server [trajectory-shard-00-02-uxufn.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to trajectory-shard-00-02-uxufn.mongodb.net:27017 closed]
2019-10-03T00:05:10.587702+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:431:11)
2019-10-03T00:05:10.587705+00:00 app[web.1]: at Pool.emit (events.js:198:13)
2019-10-03T00:05:10.587707+00:00 app[web.1]: at connect (/app/node_modules/mongodb/lib/core/connection/pool.js:580:14)
2019-10-03T00:05:10.587709+00:00 app[web.1]: at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:109:5)
2019-10-03T00:05:10.587711+00:00 app[web.1]: at runCommand (/app/node_modules/mongodb/lib/core/connection/connect.js:129:7)
2019-10-03T00:05:10.587713+00:00 app[web.1]: at Connection.errorHandler (/app/node_modules/mongodb/lib/core/connection/connect.js:321:5)
2019-10-03T00:05:10.587715+00:00 app[web.1]: at Object.onceWrapper (events.js:286:20)
2019-10-03T00:05:10.587717+00:00 app[web.1]: at Connection.emit (events.js:198:13)
2019-10-03T00:05:10.58772+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:350:12)
2019-10-03T00:05:10.587722+00:00 app[web.1]: at Object.onceWrapper (events.js:286:20)
2019-10-03T00:05:10.587724+00:00 app[web.1]: at TLSSocket.emit (events.js:198:13)
我感到困惑的部分是:
{ MongoNetworkError: failed to connect to server [trajectory-shard-00-02-uxufn.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to trajectory-shard-00-02-uxufn.mongodb.net:27017 closed]
当我在本地运行该应用程序时,一切正常。但是,部署到Heroku时出现错误,页面上唯一显示的是“未找到”。
这是我的server.js文件供参考:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const passport = require('passport');
const users = require('./routes/api/users');
var path = require('path');
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// DB config
const db = require('./config/keys').mongoURI;
// Connect to MongoDB
mongoose.connect(db, { useNewUrlParser: true })
.then(() => console.log("MongoDB database connection established successfully"))
.catch(err => console.log(err))
// Passport middleware
app.use(passport.initialize());
// Passport config
require('./config/passport')(passport);
// Routes
app.use('/api/users', users);
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'));
})
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
我已经尝试在mongoDB Atlas中将0.0.0.0/0列入白名单,但这没有用。有人对我做错的事情有任何见识吗?