[英]I have a problem in my Django project when I try to push to heroku a problem occurs
[英]I have a problem, this code works and connects into atlas cloud. But when i try to run it on heroku , it gets stuck. if its not properly structured?
下面的錯誤代碼來自 heroku 日志
process exited with status 1
heroku[web.1]: State changed from up to crashed
heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/compose host=shielded-inlet-10428.herokuapp.com fwd="66.81.177.172" dyno= connect= service= status=503 bytes= protocol=https
我設法完成這項工作並連接到雲並將數據從本地主機添加到雲,所以我沒有碰它。 因為我是新手,所以我確信我會打破它。 但是當我將它連接到 Heroku 並嘗試將數據添加到雲中並等待一段時間......它給了我 heroku 錯誤頁面。 我讀取響應數據的方式可能應該以不同的方式完成? 我試圖關注 atlas 文檔...
(我的 package.json 中確實有“開始”:“node app.js”腳本。我也有 procfile,但同樣發生了。)
也許有人會立即看到我的代碼有多么錯誤並且可以告訴我。
或者如果不是,那么問題可能會更大,我沒有修復它的技能。 然而。 但是能夠創建基於雲的應用程序將非常好。 我真的希望代碼中存在一些簡單的修復問題。
// app.js
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
var _ = require("lodash");
var posts = [];
const homeStartingContent="hello welcome..."
const app = express();
var axios = require("axios");
var data = JSON.stringify({
collection: "events",
database: "test",
dataSource: "ekaBase",
projection: {
title: 1,
content: 1,
},
});
var config = {
method: "post",
url: "xxx",
headers: {
"Content-Type": "application/json",
"Access-Control-Request-Headers": "*",
"api-key":
"xxx",
},
data: data,
};
axios(config)
.then(function (response) {
var x = response.data.documents;
x.forEach((element) => {
var cloudData = {
title: element.title,
content: element.content,
};
posts.push(cloudData);
});
})
.catch(function (error) {
console.log(error);
});
mongoose.connect(uri);
mongoose.Promise = global.Promise;
var eventSchema = mongoose.Schema({
title: String,
content: String,
});
var eventData = mongoose.model("Event", eventSchema);
//Load index page using endpoint
app.get("/", (req, res) => {
return res.render("home", {
startingContent: homeStartingContent,
posts,
});
});
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.get("/", function (req, res) {
res.render("home", { startingContent: homeStartingContent, posts });
});
/// compose page for admin
app.get("/compose", function (req, res) {
res.render("compose");
});
app.post("/compose", function (req, res) {
const post = {
title: req.body.inputTitle,
content: req.body.postBody,
};
posts.push(post);
new eventData(post)
.save()
.then((result) => {
return res.send(result); })
.catch((err) => {
return res.status(400);
});
return res.redirect("/");
});
app.get("/posts/:test", function (req, res) {
const test = _.lowerCase(req.params.test);
posts.forEach((i) => {
const storedTitle = _.lowerCase(i.title);
if (storedTitle === test) {
res.render("post", {
title: i.title,
content: i.content,
});
}
});
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function () {
console.log("Server started successfully");
});
// home.ejs
<%- include('./partials/header.ejs') %>
<div class="container">
<p><%= startingContent%></p>
<% posts.forEach(function(element){ %>
<h2><%= element.title %></h2>
<p> <%= element.content.substring(0,100)+" ..."%> <a href="/posts/<%=element.title %>">More</a>
</p>
<%});%>
<%- include('./partials/footer.ejs') %>
</div>
</div>
//compose.ejs
<%- include('./partials/header.ejs') %>
<div class="container">
<h1>Compose</h1>
<div class="form-group">
<form action="/compose" method="post">
<label for="">Title</label>
<input class="form-control"type="text" name="inputTitle" />
<label for="">Post</label>
<textarea class="form-control"name="postBody" id="" cols="30" rows="5"></textarea>
</div>
<button class="btn btn-primary"type="submit ">Publish</button></form>
<%- include('./partials/footer.ejs') %>
</div>
解決方案:調整 mongo Atlas 網絡訪問設置問題已解決。 感謝並為那個實際上不相關的問題感到抱歉,事情發生了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.