我正在尝试将React / NodeJS项目部署到Heroku。该项目使用到MongoDB的连接。我在Heroku中设置了连接字符串,如下所示:
heroku config:set MONGODB_URI = mongodb + srv:// {在此处添加用户名}:{在此处添加密码} @ nodeproject-otgtx.mongodb.net / Products_database?retryWrites = true
我的app.js如下所示:
const dotenv = require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const HttpError = require('./models/http-error');
const mongoose = require('mongoose');
const cors = require('cors');
const productRoutes = require('./routes/product-routes');
const app = express();
const MongoURI = process.env.MONGODB_URI;
const port = process.env.PORT || 5000;
app.use(cors());
app.use(bodyParser.json());
app.use((req,res,next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.header("Access-Control-Allow-Credentials", true);
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-type, Accept, Authorization');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE');
next();
});
app.use('/products', productRoutes);
app.use((req,res,next) => {
const error = new HttpError('Could not find this route.', 404);
throw error;
})
app.use((error, req, res, next) => {
if(res.headersSent) {
return next(error);
}
res.status(error.code || 500)
res.json({message: error.message || 'An unknown error occured!'});
})
app.use((error, req, res, next) => {
if(res.headerSent)
return next(error)
res.status(error.code || 500)
res.json({message:error.message || 'An unknown error occured'});
})
mongoose.connect(process.env.MONGODB_URI,{ useNewUrlParser: true }).
then(() => {app.listen(port)}).
catch(err => { console.log(err)});
在部署时出现以下错误。我在搜索类似问题时尝试了许多不同的方法,但似乎无法解决问题:
2020-01-12T06:19:09.000000+00:00 app[api]: Build succeeded
2020-01-12T06:19:12.308052+00:00 heroku[web.1]: Starting process with command `npm start`
2020-01-12T06:19:15.398090+00:00 app[web.1]:
2020-01-12T06:19:15.398121+00:00 app[web.1]: > ProductInventoryApp@1.0.0 start /app
2020-01-12T06:19:15.398124+00:00 app[web.1]: > node ./backend/app.js
2020-01-12T06:19:15.398125+00:00 app[web.1]:
2020-01-12T06:19:46.389423+00:00 app[web.1]: MongoTimeoutError: Server selection timed out after 30000 ms
2020-01-12T06:19:46.389465+00:00 app[web.1]: at Timeout._onTimeout (/app/node_modules/mongoose/node_modules/mongodb/lib/core/sdam/server_selection.js:308:9)
2020-01-12T06:19:46.389467+00:00 app[web.1]: at listOnTimeout (internal/timers.js:531:17)
2020-01-12T06:19:46.389469+00:00 app[web.1]: at processTimers (internal/timers.js:475:7) {
2020-01-12T06:19:46.389472+00:00 app[web.1]: name: 'MongoTimeoutError',
2020-01-12T06:19:46.389475+00:00 app[web.1]: reason: MongoNetworkError: connection 119 to nodeproject-shard-00-00-otgtx.mongodb.net:27017 closed
2020-01-12T06:19:46.389477+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:356:9)
2020-01-12T06:19:46.389480+00:00 app[web.1]: at Object.onceWrapper (events.js:300:26)
2020-01-12T06:19:46.389482+00:00 app[web.1]: at TLSSocket.emit (events.js:210:5)
2020-01-12T06:19:46.389484+00:00 app[web.1]: at net.js:659:12
2020-01-12T06:19:46.389486+00:00 app[web.1]: at TCP.done (_tls_wrap.js:481:7) {
2020-01-12T06:19:46.389488+00:00 app[web.1]: name: 'MongoNetworkError',
2020-01-12T06:19:46.389490+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {}
2020-01-12T06:19:46.389493+00:00 app[web.1]: },
2020-01-12T06:19:46.389495+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {}
2020-01-12T06:19:46.389497+00:00 app[web.1]: }
2020-01-12T06:19:46.780117+00:00 heroku[web.1]: State changed from crashed to starting
2020-01-12T06:19:46.776539+00:00 heroku[web.1]: State changed from starting to crashed
2020-01-12T06:19:46.760422+00:00 heroku[web.1]: Process exited with status 0
2020-01-12T06:19:48.843415+00:00 heroku[web.1]: Starting process with command `npm start`
2020-01-12T06:19:50.581739+00:00 app[web.1]:
2020-01-12T06:19:50.581755+00:00 app[web.1]: > ProductInventoryApp@1.0.0 start /app
2020-01-12T06:19:50.581758+00:00 app[web.1]: > node ./backend/app.js
2020-01-12T06:19:50.581760+00:00 app[web.1]:
2020-01-12T06:20:21.107698+00:00 app[web.1]: MongoTimeoutError: Server selection timed out after 30000 ms
2020-01-12T06:20:21.107745+00:00 app[web.1]: at Timeout._onTimeout (/app/node_modules/mongoose/node_modules/mongodb/lib/core/sdam/server_selection.js:308:9)
2020-01-12T06:20:21.107748+00:00 app[web.1]: at listOnTimeout (internal/timers.js:531:17)
2020-01-12T06:20:21.107751+00:00 app[web.1]: at processTimers (internal/timers.js:475:7) {
2020-01-12T06:20:21.107753+00:00 app[web.1]: name: 'MongoTimeoutError',
2020-01-12T06:20:21.107756+00:00 app[web.1]: reason: MongoNetworkError: connection 122 to nodeproject-shard-00-02-otgtx.mongodb.net:27017 closed
2020-01-12T06:20:21.107758+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:356:9)
2020-01-12T06:20:21.107760+00:00 app[web.1]: at Object.onceWrapper (events.js:300:26)
2020-01-12T06:20:21.107762+00:00 app[web.1]: at TLSSocket.emit (events.js:210:5)
2020-01-12T06:20:21.107764+00:00 app[web.1]: at net.js:659:12
2020-01-12T06:20:21.107767+00:00 app[web.1]: at TCP.done (_tls_wrap.js:481:7) {
2020-01-12T06:20:21.107768+00:00 app[web.1]: name: 'MongoNetworkError',
2020-01-12T06:20:21.107770+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {}
2020-01-12T06:20:21.107772+00:00 app[web.1]: },
2020-01-12T06:20:21.107773+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {}
2020-01-12T06:20:21.107775+00:00 app[web.1]: }
2020-01-12T06:20:21.518805+00:00 heroku[web.1]: State changed from starting to crashed
2020-01-12T06:20:21.496469+00:00 heroku[web.1]: Process exited with status 0
2020-01-12T06:20:22.805485+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=product-inventory-app.herokuapp.com request_id=fd93ca8c-9c4f-410d-9c80-1603183a3c11 fwd="70.95.154.168" dyno= connect= service= status=503 bytes= protocol=http
2020-01-12T06:20:24.435916+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=product-inventory-app.herokuapp.com request_id=02f8e276-cc81-45e4-80ef-4fbbfd651e1f fwd="70.95.154.168" dyno= connect= service= status=503 bytes= protocol=http
2020-01-12T06:20:25.140317+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=product-inventory-app.herokuapp.com request_id=6001ff0d-f020-4cd9-bd91-11f5f7efd235 fwd="70.95.154.168" dyno= connect= service= status=503 bytes= protocol=http
2020-01-12T06:20:25.678028+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=product-inventory-app.herokuapp.com request_id=6da70469-35ac-4294-b849-13f198f479d3 fwd="70.95.154.168" dyno= connect= service= status=503 bytes= protocol=http
2020-01-12T06:20:47.056820+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=product-inventory-app.herokuapp.com request_id=4b416725-768c-4992-8cba-3a080819c0a7 fwd="70.95.154.168" dyno= connect= service= status=503 bytes= protocol=http