首页 > 解决方案 > 在 heroku 上使用 express 和 mongodb 部署 nuxt.js 的问题

问题描述

所以我在heroku上部署我的应用程序有问题。我在后端使用带有 express.js 的 nuxt 制作了应用程序。它使用 mongod 作为数据库。loalhost 上一切正常,但是当我按照本教程进行操作时: https ://medium.com/@ale_colombo/deploy-nuxt-js-app-on-heroku-531a935bce90

我的应用程序无法在生产环境中运行。在主页上显示服务器错误。我不知道出了什么问题。我是第一次部署 nuxt.js 应用程序。

日志heroku显示:

2020-06-15T07:11:18.000000+00:00 app[api]: Build succeeded
2020-06-15T07:11:18.902707+00:00 heroku[web.1]: Starting process with command `npm start`
2020-06-15T07:11:24.260338+00:00 app[web.1]: 
2020-06-15T07:11:24.260358+00:00 app[web.1]: > pai@1.0.0 start /app
2020-06-15T07:11:24.260359+00:00 app[web.1]: > cross-env NODE_ENV=production node server/index.js
2020-06-15T07:11:24.260360+00:00 app[web.1]: 
2020-06-15T07:11:25.624765+00:00 app[web.1]: 
2020-06-15T07:11:25.624795+00:00 app[web.1]: WARN  No .env file found in /app.
2020-06-15T07:11:25.624796+00:00 app[web.1]: 
2020-06-15T07:11:25.721532+00:00 app[web.1]: 
2020-06-15T07:11:25.721536+00:00 app[web.1]: READY  Server listening on http://urlhere
2020-06-15T07:11:25.721537+00:00 app[web.1]: 
2020-06-15T07:11:25.838724+00:00 heroku[web.1]: State changed from starting to up
2020-06-15T07:11:44.168573+00:00 app[web.1]: 
2020-06-15T07:11:44.168585+00:00 app[web.1]: ERROR  connect ECONNREFUSED 127.0.0.1:3000
2020-06-15T07:11:44.168585+00:00 app[web.1]: 
2020-06-15T07:11:44.168589+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
2020-06-15T07:11:44.168590+00:00 app[web.1]: 
2020-06-15T07:11:44.178096+00:00 heroku[router]: at=info method=GET path="/" host=safe-fortress-45777.herokuapp.com request_id=e6186311-46a7-471c-854f-fc42dd8e5266 fwd="***" dyno=web.1 connect=1ms service=92ms status=500 bytes=1228 protocol=https

有连接错误,但我不知道如何修复它。我找不到任何解决方案。起初我有 Procfile 但我在某处读到它不是必需的但删除它后应用程序也无法工作。

那是我的 server/index.js 文件:

const express = require("express");
const consola = require("consola");
const { Nuxt, Builder } = require("nuxt");
const mongoose = require("mongoose");
require("dotenv").config();

mongoose.connect(process.env.MONGODB_URL, {
  useNewUrlParser: true,
  useUnifiedTopology: true
});

const app = express();
app.use(express.json());

// Import and Set Nuxt.js options
const config = require("../nuxt.config.js");
config.dev = process.env.NODE_ENV !== "production";

async function start() {
  // Init Nuxt.js
  const nuxt = new Nuxt(config);

  const { host, port } = nuxt.options.server;

  await nuxt.ready();
  // Build only in dev mode
  if (config.dev) {
    const builder = new Builder(nuxt);
    await builder.build();
  }

  // Give nuxt middleware to express
  app.use("/api", require("./router"));
  app.use(nuxt.render);

  // Listen the server
  app.listen(port, host);
  consola.ready({
    message: `Server listening on http://${host}:${port}`,
    badge: true
  });
}
start();

标签: javascriptnode.jsexpressherokunuxt.js

解决方案


推荐阅读