【发布时间】:2020-05-20 22:18:35
【问题描述】:
我已经在这部分苦苦挣扎了大约 10 多个小时。我尝试对此进行研究并发现问题。我尝试了一些解决方案,但似乎无法让它为我工作。我已经按照 BradTraversy 的方式进行了所有设置,但由于某种原因,我的没有部署。本地开发在事物的前端和后端都可以正常工作。
问题是当我将生产部署到 Heroku 上时。它显示了前端,但随后后端服务器决定不工作。我将在此行之后将存储库发布到存储库。
非常感谢你们花时间帮助我。
存储库:https://github.com/bradtraversy/devconnector_2.0
我通过输入将应用程序部署到 Heroku:
git push heroku master
然后我输入以下命令检查日志:
heroku logs --tail
我在此日志中遇到的错误是:
heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/users service=10164ms status=503"
和
heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/api/users" service= status=503
2020-02-05T02:14:30.248090+00:00 heroku[web.1]: State changed from up to crashed
2020-02-05T02:14:30.225433+00:00 heroku[web.1]: Process exited with status 1
2020-02-05T02:24:44.491342+00:00 heroku[web.1]: State changed from crashed to starting
2020-02-05T02:24:52.827814+00:00 heroku[web.1]: Starting process with command `npm start`
2020-02-05T02:24:55.035533+00:00 app[web.1]:
2020-02-05T02:24:55.035564+00:00 app[web.1]: > devconnector@1.0.0 start /app
2020-02-05T02:24:55.035566+00:00 app[web.1]: > node server.js
2020-02-05T02:24:55.035568+00:00 app[web.1]:
2020-02-05T02:24:55.719810+00:00 app[web.1]: Server started on port 58394
2020-02-05T02:24:56.130106+00:00 heroku[web.1]: State changed from starting to up
2020-02-05T02:25:25.780242+00:00 app[web.1]: Server selection timed out after 30000 ms
2020-02-05T02:25:25.798194+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-02-05T02:25:25.798598+00:00 app[web.1]: npm ERR! errno 1
2020-02-05T02:25:25.799936+00:00 app[web.1]: npm ERR! devconnector@1.0.0 start: `node server.js`
2020-02-05T02:25:25.800096+00:00 app[web.1]: npm ERR! Exit status 1
2020-02-05T02:25:25.800260+00:00 app[web.1]: npm ERR!
2020-02-05T02:25:25.800376+00:00 app[web.1]: npm ERR! Failed at the devconnector@1.0.0 start script.
2020-02-05T02:25:25.800502+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-02-05T02:25:25.809573+00:00 app[web.1]:
2020-02-05T02:25:25.809899+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-02-05T02:25:25.810391+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-02-05T02_25_25_801Z-debug.log
2020-02-05T02:25:25.929540+00:00 heroku[web.1]: State changed from up to crashed
2020-02-05T02:25:25.910090+00:00 heroku[web.1]: Process exited with status 1
2020-02-05T02:26:34.034101+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/api/users" host=floating-eyrie-69630.herokuapp.com request_id=c4fd2b74-4a3b-4857-b954-81ee18c94b8a fwd="71.178.243.54" dyno= connect= service= status=503 bytes= protocol=https
2020-02-05T02:30:24.143711+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/users" host=floating-eyrie-69630.herokuapp.com request_id=306e3957-63bc-4927-a8d7-d92cfd73790d fwd="71.178.243.54" dyno=web.1 connect=0ms service=8196ms status=503 bytes=0 protocol=https
2020-02-05T02:30:24.137543+00:00 app[web.1]: Server selection timed out after 30000 ms
2020-02-05T02:30:24.146314+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-02-05T02:30:24.146527+00:00 app[web.1]: npm ERR! errno 1
2020-02-05T02:30:24.147415+00:00 app[web.1]: npm ERR! devconnector@1.0.0 start: `node server.js`
2020-02-05T02:30:24.147505+00:00 app[web.1]: npm ERR! Exit status 1
2020-02-05T02:30:24.147637+00:00 app[web.1]: npm ERR!
2020-02-05T02:30:24.147718+00:00 app[web.1]: npm ERR! Failed at the devconnector@1.0.0 start script.
2020-02-05T02:30:24.147802+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-02-05T02:30:24.153368+00:00 app[web.1]:
2020-02-05T02:30:24.153531+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-02-05T02:30:24.153633+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-02-05T02_30_24_148Z-debug.log
2020-02-05T02:30:24.230309+00:00 heroku[web.1]: State changed from up to crashed
2020-02-05T02:30:24.141581+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/users" host=floating-eyrie-69630.herokuapp.com request_id=810181a6-17d8-4718-a417-18e54decccdd fwd="71.178.243.54" dyno=web.1 connect=0ms service=10164ms status=503 bytes=0 protocol=https
2020-02-05T02:30:24.143736+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/users" host=floating-eyrie-69630.herokuapp.com request_id=ea00ea5c-3403-4db9-8df6-d895c612dc3b fwd="71.178.243.54" dyno=web.1 connect=0ms service=8826ms status=503 bytes=0 protocol=https
2020-02-05T02:30:24.142003+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/users" host=floating-eyrie-69630.herokuapp.com request_id=62b8da0b-d894-4d38-9c2d-496833c0ec14 fwd="71.178.243.54" dyno=web.1 connect=0ms service=9308ms status=503 bytes=0 protocol=https
2020-02-05T02:30:24.212393+00:00 heroku[web.1]: Process exited with status 1
只需注意几件事,我的所有设置都正确。我设置了正确输入的用户名和密码的 mongoURI
config/production.json
{
"mongoURI": "mongodb+srv://myusername:mypassword@cluster0-ziuao.mongodb.net/test?retryWrites=true&w=majority",
"jwtSecret": "secret",
"githubClientId": "",
"githubSecret": ""
}
配置/数据库
const mongoose = require('mongoose');
const config = require('config');
const db = config.get('mongoURI');
const connectDB = async () => {
try {
await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
});
console.log('MongoDB Connected...');
} catch (err) {
console.error(err.message);
// Exit process with failure
process.exit(1);
}
};
module.exports = connectDB;
server.js
const express = require('express');
const connectDB = require('./config/db');
const path = require('path');
const app = express();
// Connect Database
connectDB();
// Init Middleware
app.use(express.json({ extended: false }));
// Define Routes
app.use('/api/users', require('./routes/api/users'));
app.use('/api/auth', require('./routes/api/auth'));
app.use('/api/profile', require('./routes/api/profile'));
app.use('/api/posts', require('./routes/api/posts'));
// Serve static assets in production
if (process.env.NODE_ENV === 'production') {
// Set static folder
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}`));
我想知道这可能是什么原因造成的?任何有用的答案和/或推动正确的方向将不胜感激!谢谢!
【问题讨论】:
-
我认为问题出在路由路径上,因为像
./routes/api/authheroku 这样的路由可能无法理解。您必须使用带有 __dir 的路径。尝试更改路径并检查会发生什么。
标签: node.js mongodb express heroku mern