javascript - 在 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();
解决方案
推荐阅读
- c# - 尝试在调试中查看属性会在时间延迟后给出不同的计数。这是因为延期执行吗?
- java - SFTP 连接器 DH 密钥错误
- c# - 执行单元测试时 C# 配置管理器引用 machine.config 而不是 app.config
- node.js - 当 headers 为 true 时,fast-csv 返回未定义的数据
- java - 单击按钮(清单按钮)后,“应用程序名称”已停止
- ios - WatchKit 接口控制器之间的动画
- linux - AWS Linux CloudWatch - 前 5 个进程和消耗的内存的指标
- openlayers - 如何在 OpenLayers 地图版本 4.6.4 上触发、模拟或调度点击事件?
- javascript - 当我在 Electron 应用程序中双击打开文件时,Argv[1] 返回意外值
- powershell - 更改当前正在运行的脚本