将Express服务器部署到Heroku

时间:2020-07-23 13:12:07

标签: node.js mongodb express heroku mongoose

我正在使用node,express和mongodb,mongoose创建我的第一个api,并尝试将其部署到Heroku,但是网站响应为“ Application Error”。我在开发中进行了测试,一切正常,在与邮递员进行测试时,它将新数据添加到mongodb集合中。此外,.env文件的用途是什么,它在其中写入TIMES= 2(我从heroku指南中复制了它)

这是我的文件结构:

node_modules
.env
.gitignore
index.js
package.json
package-lock.json
Procfile
ProfileSchema.js

这是我的index.js文件: `

const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const app = express();
const PORT = process.env.PORT || 5000;
const Profile = require("./ProfileSchema");

app.use(bodyParser.json());

app.get("/", async (req, res) => {
  try {
    const profile = await Profile.find();
    res.json(profile);
  } catch (error) {
    res.json({ message: error });
  }
});

app.post("/", async (req, res) => {
  try {
    const profile = new Profile({
      first_name: req.body.first_name,
      last_name: req.body.last_name,
      age: req.body.age,
    });
    const savedProfile = await profile.save();
    res.send(savedProfile);
  } catch (error) {
    res.json({ message: error });
  }
});

mongoose.connect(
  "MY_MONGODB_URL",
  { useNewUrlParser: true, useUnifiedTopology: true },
  () => console.log("connected to db")
);

app.listen(PORT);

这是heroku日志:

 2020-07-23T12:30:51.289767+00:00 app[api]: Initial release by user nguyenduy010798@gmail.com
2020-07-23T12:30:51.289767+00:00 app[api]: Release v1 created by user nguyenduy010798@gmail.com
2020-07-23T12:30:51.506259+00:00 app[api]: Enable Logplex by user nguyenduy010798@gmail.com    
2020-07-23T12:30:51.506259+00:00 app[api]: Release v2 created by user nguyenduy010798@gmail.com
2020-07-23T12:32:09.000000+00:00 app[api]: Build started by user nguyenduy010798@gmail.com     
2020-07-23T12:32:27.458923+00:00 app[api]: Deploy 72a72445 by user nguyenduy010798@gmail.com     
2020-07-23T12:32:27.458923+00:00 app[api]: Release v3 created by user nguyenduy010798@gmail.com  
2020-07-23T12:32:27.479763+00:00 app[api]: Scaled to web@1:Free by user nguyenduy010798@gmail.com
2020-07-23T12:32:28.000000+00:00 app[api]: Build succeeded
2020-07-23T12:32:29.953637+00:00 heroku[web.1]: Starting process with command `node index.js`    
2020-07-23T12:32:33.742437+00:00 heroku[web.1]: State changed from starting to up
2020-07-23T12:33:03.177448+00:00 app[web.1]: connected to db
2020-07-23T12:33:09.522028+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=profile-api-test.herokuapp.com request_id=52e04478-ea91-438c-a2f8-a65f7f90a744 fwd="88.114.118.158" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=https
2020-07-23T12:33:12.540769+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=profile-api-test.herokuapp.com request_id=85093811-9869-462f-bdb4-77baefc81dba fwd="88.114.118.158" dyno=web.1 connect=1ms service=5ms status=404 bytes=394 protocol=https
2020-07-23T12:34:20.000000+00:00 app[api]: Build started by user nguyenduy010798@gmail.com
2020-07-23T12:34:35.373109+00:00 app[api]: Release v4 created by user nguyenduy010798@gmail.com
2020-07-23T12:34:35.373109+00:00 app[api]: Deploy 15055919 by user nguyenduy010798@gmail.com
2020-07-23T12:34:35.571911+00:00 heroku[web.1]: Restarting
2020-07-23T12:34:35.584939+00:00 heroku[web.1]: State changed from up to starting
2020-07-23T12:34:36.000000+00:00 app[api]: Build succeeded
2020-07-23T12:34:36.434807+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-07-23T12:34:36.517469+00:00 heroku[web.1]: Process exited with status 143
2020-07-23T12:34:37.594567+00:00 heroku[web.1]: Starting process with command `node index.js`
2020-07-23T12:34:40.338573+00:00 heroku[web.1]: State changed from starting to up
2020-07-23T12:35:09.955534+00:00 app[web.1]: connected to db
2020-07-23T12:35:17.799558+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=profile-api-test.herokuapp.com request_id=26b80a83-ec63-4ce4-a867-56de75c0076c fwd="88.114.118.158" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https
2020-07-23T12:38:01.000000+00:00 app[api]: Build started by user nguyenduy010798@gmail.com
2020-07-23T12:38:16.620755+00:00 app[api]: Release v5 created by user nguyenduy010798@gmail.com
2020-07-23T12:38:16.620755+00:00 app[api]: Deploy cf51749b by user nguyenduy010798@gmail.com
2020-07-23T12:38:17.000000+00:00 app[api]: Build succeeded
2020-07-23T12:38:17.768460+00:00 heroku[web.1]: Restarting
2020-07-23T12:38:17.786254+00:00 heroku[web.1]: State changed from up to starting
2020-07-23T12:38:18.602665+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-07-23T12:38:18.680094+00:00 heroku[web.1]: Process exited with status 143
2020-07-23T12:38:19.833257+00:00 heroku[web.1]: Starting process with command `node index.js`
2020-07-23T12:38:22.674445+00:00 heroku[web.1]: State changed from starting to up
2020-07-23T12:38:52.507726+00:00 app[web.1]: connected to db
2020-07-23T12:39:19.098378+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=profile-api-test.herokuapp.com request_id=b6e732e4-e752-47e3-a92f-9da6d057343d fwd="88.114.118.158" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=https
2020-07-23T12:46:32.000000+00:00 app[api]: Build started by user nguyenduy010798@gmail.com
2020-07-23T12:46:58.644169+00:00 app[api]: Deploy 60e7037e by user nguyenduy010798@gmail.com
2020-07-23T12:46:58.644169+00:00 app[api]: Release v6 created by user nguyenduy010798@gmail.com
2020-07-23T12:46:59.000000+00:00 app[api]: Build succeeded
2020-07-23T12:46:59.019213+00:00 heroku[web.1]: Restarting
2020-07-23T12:46:59.034423+00:00 heroku[web.1]: State changed from up to starting
2020-07-23T12:46:59.903631+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-07-23T12:46:59.972752+00:00 heroku[web.1]: Process exited with status 143
2020-07-23T12:47:01.327683+00:00 heroku[web.1]: Starting process with command `node index.js`
2020-07-23T12:47:05.681920+00:00 heroku[web.1]: State changed from starting to up
2020-07-23T12:47:12.129857+00:00 heroku[router]: at=info method=GET path="/" host=profile-api-test.herokuapp.com request_id=25753136-0d54-4ddb-89c9-ca45e8a96734 fwd="88.114.118.158" dyno=web.1 connect=1ms service=10ms status=200 bytes=203 protocol=https
2020-07-23T12:47:34.466683+00:00 app[web.1]: connected to db
2020-07-23T12:48:17.648642+00:00 heroku[router]: at=info method=GET path="/" host=profile-api-test.herokuapp.com request_id=4107eb3d-16e1-4cfe-9f0b-1bc36007bd8f fwd="88.114.118.158" dyno=web.1 connect=0ms service=5ms status=304 bytes=149 protocol=https
2020-07-23T12:48:19.822379+00:00 heroku[router]: at=info method=GET path="/" host=profile-api-test.herokuapp.com request_id=f95ba931-4bfb-4540-9b06-add58995b047 fwd="88.114.118.158" dyno=web.1 connect=0ms service=2ms status=304 bytes=149 protocol=https
2020-07-23T12:50:14.000000+00:00 app[api]: Build started by user nguyenduy010798@gmail.com
2020-07-23T12:50:30.000000+00:00 app[api]: Build succeeded
2020-07-23T12:50:30.067786+00:00 app[api]: Release v7 created by user nguyenduy010798@gmail.com
2020-07-23T12:50:30.067786+00:00 app[api]: Deploy daa00e82 by user nguyenduy010798@gmail.com
2020-07-23T12:50:30.263283+00:00 heroku[web.1]: Restarting
2020-07-23T12:50:30.265119+00:00 heroku[web.1]: State changed from up to starting
2020-07-23T12:50:31.572749+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-07-23T12:50:31.683233+00:00 heroku[web.1]: Process exited with status 143
2020-07-23T12:50:33.513994+00:00 heroku[web.1]: Starting process with command `node index.js`
2020-07-23T12:50:38.367972+00:00 heroku[web.1]: State changed from starting to up
2020-07-23T12:51:08.185735+00:00 app[web.1]: connected to db
2020-07-23T12:51:10.056487+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=profile-api-test.herokuapp.com request_id=5d1dd483-373e-4823-89b7-37b1c8340ba6 fwd="88.114.118.158" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=https
2020-07-23T12:51:32.839056+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=profile-api-test.herokuapp.com request_id=22314fb2-d3ee-4dad-a744-594135530f39 fwd="88.114.118.158" dyno=web.1 connect=1ms service=30007ms status=503 bytes=0 protocol=https

0 个答案:

没有答案