Node.js 将MERN应用程序部署到Heroku时出错

Node.js 将MERN应用程序部署到Heroku时出错,node.js,reactjs,mongodb,express,heroku,Node.js,Reactjs,Mongodb,Express,Heroku,应用程序已成功部署,但当我尝试打开应用程序时,出现以下错误: 应用程序错误 应用程序中出现错误,无法提供您的页面。如果您是应用程序所有者,请查看日志以了解详细信息。您可以使用以下命令从Heroku CLI执行此操作 heroku原木——尾巴 我运行了-heroku日志--tail,得到以下结果: deons-mbp:shrinkURL deonchoi$ heroku logs --tail 2019-12-13T02:59:25.356685+00:00 app[web.1]: npm E

应用程序已成功部署,但当我尝试打开应用程序时,出现以下错误:


应用程序错误 应用程序中出现错误,无法提供您的页面。如果您是应用程序所有者,请查看日志以了解详细信息。您可以使用以下命令从Heroku CLI执行此操作 heroku原木——尾巴


我运行了-heroku日志--tail,得到以下结果:

deons-mbp:shrinkURL deonchoi$ heroku logs --tail
2019-12-13T02:59:25.356685+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-12-13T02:59:25.356952+00:00 app[web.1]: npm ERR! errno 1
2019-12-13T02:59:25.357905+00:00 app[web.1]: npm ERR! backend@1.0.0 start: `node server.js`
2019-12-13T02:59:25.358057+00:00 app[web.1]: npm ERR! Exit status 1
2019-12-13T02:59:25.358220+00:00 app[web.1]: npm ERR!
2019-12-13T02:59:25.358348+00:00 app[web.1]: npm ERR! Failed at the backend@1.0.0 start script.
2019-12-13T02:59:25.358480+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-12-13T02:59:25.362742+00:00 app[web.1]: 
2019-12-13T02:59:25.362837+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-12-13T02:59:25.362931+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-12-13T02_59_25_359Z-debug.log
2019-12-13T02:59:25.446611+00:00 heroku[web.1]: State changed from starting to crashed
2019-12-13T02:59:25.422394+00:00 heroku[web.1]: Process exited with status 1
2019-12-13T03:00:38.195196+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=shrinkurlapp.herokuapp.com request_id=99378587-a922-4ad8-8445-aca15eba268a fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:00:38.710170+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=shrinkurlapp.herokuapp.com request_id=ba6a84fe-9a4b-433d-a00d-1a6231e0317c fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:00:49.612623+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=shrinkurlapp.herokuapp.com request_id=7fb228bc-9e62-4526-a51e-feff9631e266 fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:00:50.201204+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=shrinkurlapp.herokuapp.com request_id=ada57278-8d33-4569-b3e3-bad6b136a234 fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:03:11.014926+00:00 heroku[web.1]: State changed from crashed to starting
2019-12-13T03:03:13.149581+00:00 heroku[web.1]: Starting process with command `npm start`
2019-12-13T03:03:15.545496+00:00 app[web.1]: 
2019-12-13T03:03:15.545520+00:00 app[web.1]: > backend@1.0.0 start /app
2019-12-13T03:03:15.545522+00:00 app[web.1]: > node server.js
2019-12-13T03:03:15.545524+00:00 app[web.1]: 
2019-12-13T03:03:16.082886+00:00 app[web.1]: 
2019-12-13T03:03:16.082936+00:00 app[web.1]: /app/node_modules/mongoose/lib/connection.js:541
2019-12-13T03:03:16.082939+00:00 app[web.1]: throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
2019-12-13T03:03:16.082942+00:00 app[web.1]: ^
2019-12-13T03:03:16.086177+00:00 app[web.1]: Error [MongooseError]: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
2019-12-13T03:03:16.086180+00:00 app[web.1]: at new MongooseError (/app/node_modules/mongoose/lib/error/mongooseError.js:10:11)
2019-12-13T03:03:16.086183+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:541:11)
2019-12-13T03:03:16.086185+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:328:15)
2019-12-13T03:03:16.086187+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:16:10)
2019-12-13T03:03:16.086189+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:959:30)
2019-12-13T03:03:16.086191+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
2019-12-13T03:03:16.086193+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:815:32)
2019-12-13T03:03:16.086195+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:727:14)
2019-12-13T03:03:16.086197+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
2019-12-13T03:03:16.086200+00:00 app[web.1]: at internal/main/run_main_module.js:17:11 {
2019-12-13T03:03:16.086203+00:00 app[web.1]: message: 'The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.',
2019-12-13T03:03:16.086205+00:00 app[web.1]: name: 'MongooseError'
2019-12-13T03:03:16.086207+00:00 app[web.1]: }
2019-12-13T03:03:16.092626+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-12-13T03:03:16.092996+00:00 app[web.1]: npm ERR! errno 1
2019-12-13T03:03:16.094046+00:00 app[web.1]: npm ERR! backend@1.0.0 start: `node server.js`
2019-12-13T03:03:16.094306+00:00 app[web.1]: npm ERR! Exit status 1
2019-12-13T03:03:16.094539+00:00 app[web.1]: npm ERR!
2019-12-13T03:03:16.094740+00:00 app[web.1]: npm ERR! Failed at the backend@1.0.0 start script.
2019-12-13T03:03:16.094919+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-12-13T03:03:16.104750+00:00 app[web.1]: 
2019-12-13T03:03:16.105020+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-12-13T03:03:16.105275+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-12-13T03_03_16_095Z-debug.log
2019-12-13T03:03:16.178860+00:00 heroku[web.1]: State changed from starting to crashed
2019-12-13T03:03:16.164865+00:00 heroku[web.1]: Process exited with status 1
2019-12-13T03:03:24.145385+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=shrinkurlapp.herokuapp.com request_id=ca564a1c-156c-484a-9354-ce243b3b1ed5 fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:03:24.586560+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=shrinkurlapp.herokuapp.com request_id=bb81ead2-cd04-462f-9ab9-64b84d9238bb fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:08:40.000000+00:00 app[api]: Build started by user deon.choi@gmail.com
2019-12-13T03:08:55.468949+00:00 app[api]: Deploy 67508aba by user deon.choi@gmail.com
2019-12-13T03:08:55.468949+00:00 app[api]: Release v4 created by user deon.choi@gmail.com
2019-12-13T03:08:55.741086+00:00 heroku[web.1]: State changed from crashed to starting
2019-12-13T03:08:56.000000+00:00 app[api]: Build succeeded
2019-12-13T03:08:57.971434+00:00 heroku[web.1]: Starting process with command `npm start`
2019-12-13T03:08:59.915432+00:00 app[web.1]: 
2019-12-13T03:08:59.915464+00:00 app[web.1]: > backend@1.0.0 start /app
2019-12-13T03:08:59.915467+00:00 app[web.1]: > node server.js
2019-12-13T03:08:59.915470+00:00 app[web.1]: 
2019-12-13T03:09:00.319828+00:00 app[web.1]: 
2019-12-13T03:09:00.319851+00:00 app[web.1]: /app/node_modules/mongoose/lib/connection.js:541
2019-12-13T03:09:00.319854+00:00 app[web.1]: throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
2019-12-13T03:09:00.319857+00:00 app[web.1]: ^
2019-12-13T03:09:00.322625+00:00 app[web.1]: Error [MongooseError]: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
2019-12-13T03:09:00.322629+00:00 app[web.1]: at new MongooseError (/app/node_modules/mongoose/lib/error/mongooseError.js:10:11)
2019-12-13T03:09:00.322632+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:541:11)
2019-12-13T03:09:00.322634+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:328:15)
2019-12-13T03:09:00.322636+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:18:10)
2019-12-13T03:09:00.322639+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:959:30)
2019-12-13T03:09:00.322641+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
2019-12-13T03:09:00.322644+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:815:32)
2019-12-13T03:09:00.322646+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:727:14)
2019-12-13T03:09:00.322648+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
2019-12-13T03:09:00.322651+00:00 app[web.1]: at internal/main/run_main_module.js:17:11 {
2019-12-13T03:09:00.322654+00:00 app[web.1]: message: 'The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.',
2019-12-13T03:09:00.322657+00:00 app[web.1]: name: 'MongooseError'
2019-12-13T03:09:00.322660+00:00 app[web.1]: }
2019-12-13T03:09:00.331200+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-12-13T03:09:00.331487+00:00 app[web.1]: npm ERR! errno 1
2019-12-13T03:09:00.333012+00:00 app[web.1]: npm ERR! backend@1.0.0 start: `node server.js`
2019-12-13T03:09:00.333312+00:00 app[web.1]: npm ERR! Exit status 1
2019-12-13T03:09:00.333487+00:00 app[web.1]: npm ERR!
2019-12-13T03:09:00.333633+00:00 app[web.1]: npm ERR! Failed at the backend@1.0.0 start script.
2019-12-13T03:09:00.333752+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-12-13T03:09:00.341621+00:00 app[web.1]: 
2019-12-13T03:09:00.341770+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-12-13T03:09:00.341887+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-12-13T03_09_00_334Z-debug.log
2019-12-13T03:09:00.424560+00:00 heroku[web.1]: State changed from starting to crashed
2019-12-13T03:09:00.403174+00:00 heroku[web.1]: Process exited with status 1
2019-12-13T03:09:01.483310+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=shrinkurlapp.herokuapp.com request_id=79b75979-00fa-4994-a5d1-4f73c5841172 fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:09:02.191776+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=shrinkurlapp.herokuapp.com request_id=93cf4327-3365-4174-a2df-7545952b00a6 fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:09:03.076316+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=shrinkurlapp.herokuapp.com request_id=a2492372-c679-4144-acd1-7956692ad1e0 fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:09:03.469741+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=shrinkurlapp.herokuapp.com request_id=d14e6638-96d5-4f19-8701-03ab766969fc fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:09:47.050280+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=shrinkurlapp.herokuapp.com request_id=287c82b4-b3a6-42cc-81c5-ed97f310f7d3 fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https
2019-12-13T03:09:47.349496+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=shrinkurlapp.herokuapp.com request_id=b1ced555-16fd-4255-92be-486d0917aff1 fwd="98.210.22.60" dyno= connect= service= status=503 bytes= protocol=https

请参阅以下内容以设置要使用的环境变量:


需要设置的env变量是:
DB\u CONNECTION

问题在于您的进程。env.DB\u CONNECTION。。。。。当前未在HEROKU部署环境中设置DB_连接密钥。从日志中的以下行可以观察到错误:2019-12-13T03:03:16.082939+00:00应用程序[web.1]:抛出新MongooseError(
uri
参数到
openUri()
必须是'+
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const path = require('path');

require('dotenv').config();

const app = express();
const port = process.env.PORT || 3000;

app.use(cors());
app.use(express.json());
app.use(cookieParser());

const dbURI = process.env.DB_CONNECTION;

mongoose.connect(dbURI, {useNewUrlParser: true, useUnifiedTopology: true});

const connection = mongoose.connection;
connection.once('open', () => {
    console.log('MongoDB database connection established successfully');
});

const urlsRouter = require('./routes/urls');
const redirectRouter = require('./routes/index');
const authRoute = require('./routes/auth');

app.use('/api/urls', urlsRouter);
app.use('/api/user', authRoute);
app.use('/api', redirectRouter);

app.use(express.static(path.join(__dirname, 'build')));

app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});

app.listen(port, () => {
    console.log(`Server is running on port ${port}`)
});