Error code below is from heroku log
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
I managed to get this work and connect to cloud and add data to cloud from localhost, so i didn't touch it. As i am new in this i was sure i'd break it. But when i connect it to Heroku and try to add data into cloud and wait a while... it gives me heroku error page. The way i read the response data maybe it should be done differently? i was trying to follow atlas docs...
( I do have "start": "node app.js" script in my package.json. also i had procfile but the same happened.)
maybe someone sees right away how wrong my code is and could tell me.
or if not then the problem might be bigger and i have no skills to repaire this. Yet! but to be able to create cloud based app would be really nice. I really hope theres just some easy fix problem in code.
// 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>