为啥即使我的数据库已连接,我的 Heroku 应用程序在其他设备中打开时也没有加载任何数据?
Posted
技术标签:
【中文标题】为啥即使我的数据库已连接,我的 Heroku 应用程序在其他设备中打开时也没有加载任何数据?【英文标题】:Why my Heroku app is not loading any data while opening in other devices even though my database is connected?为什么即使我的数据库已连接,我的 Heroku 应用程序在其他设备中打开时也没有加载任何数据? 【发布时间】:2022-01-11 13:56:19 【问题描述】:我最近从 git 在 heroku 上部署了我的 NodeJS 应用程序,它成功部署并在我的本地机器上运行,没有任何失败。但是在其他设备上打开时,存储在数据库中的数据没有加载。我还允许我的 atlas IP 白名单从所有设备访问。我找不到错误。 这是我的 App.js 文件,
require("dotenv").config();
const mongoose = require("mongoose");
const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const path = require("path");
const app = express();
const authRoutes = require("./routes/auth");
const userRoutes = require("./routes/user");
const categoryRoutes = require("./routes/category");
const productRoutes = require("./routes/product");
const orderRoutes = require("./routes/order");
const stripeRoutes = require("./routes/stripepayment");
//Middle wares
app.use(bodyParser.json( limit: "50mb" ));
app.use(bodyParser.urlencoded( limit: "50mb", extended: true ));
app.use(cookieParser());
app.use(cors());
// Database connection
mongoose
.connect(process.env.DATABASE_URL,
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
)
.then(() =>
console.log("DB CONNECTED");
)
.catch((err) =>
console.log("ERROR", err);
);
//Routes
app.use("/api", authRoutes);
app.use("/api", userRoutes);
app.use("/api", categoryRoutes);
app.use("/api", productRoutes);
app.use("/api", orderRoutes);
app.use("/api", stripeRoutes);
if (process.env.NODE_ENV === "production")
//Set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) =>
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
);
//server
app.listen(process.env.PORT || 7000, "0.0.0.0", () =>
console.log(`App is running at 7000`);
);
这是我的 Heroku 应用程序日志
2021-12-06T09:52:16.430092+00:00 heroku[router]: at=info method=GET path="/static/css/2.e7166e2e.chunk.css" host=shop-spot.herokuapp.com request_id=25aa7dc1-acf3-4b9b-af2a-f4ffe0948dc5 fwd="157.48.225.94" dyno=web.1 connect=0ms service=1ms status=304 bytes=270 protocol=https
2021-12-06T09:52:16.450752+00:00 heroku[router]: at=info method=GET path="/static/css/main.2bcfeed7.chunk.css" host=shop-spot.herokuapp.com request_id=c565960a-7fa9-4aa9-8498-564ff9411eb6 fwd="157.48.225.94" dyno=web.1 connect=0ms service=1ms status=304 bytes=270 protocol=https
2021-12-06T09:52:16.741237+00:00 heroku[router]: at=info method=GET path="/static/js/main.674c3ee4.chunk.js" host=shop-spot.herokuapp.com request_id=20ff86ee-d3fa-44d0-b29c-6fbbb63869ef fwd="157.48.225.94" dyno=web.1 connect=0ms service=1ms status=304 bytes=270 protocol=https
2021-12-06T09:52:16.785859+00:00 heroku[router]: at=info method=GET path="/static/js/2.53d21ff9.chunk.js" host=shop-spot.herokuapp.com request_id=637d2007-8195-41ed-be1b-1da51fb7a08f fwd="157.48.225.94" dyno=web.1 connect=0ms service=1ms status=304 bytes=271 protocol=https
2021-12-06T09:52:19.384480+00:00 heroku[router]: at=info method=GET path="/weblogo.png" host=shop-spot.herokuapp.com request_id=15681d65-512d-407c-b8cd-8b7743ce2e4b fwd="157.48.225.94" dyno=web.1 connect=0ms service=2ms status=304 bytes=270 protocol=https
2021-12-06T09:52:20.252200+00:00 heroku[router]: at=info method=GET path="/manifest.json" host=shop-spot.herokuapp.com request_id=77dff797-2561-4136-b3cc-75ff247c9129 fwd="157.48.225.94" dyno=web.1 connect=0ms service=10ms status=304 bytes=269 protocol=https
2021-12-06T09:52:20.596998+00:00 heroku[router]: at=info method=GET path="/logo192.png" host=shop-spot.herokuapp.com request_id=a9d544d3-8be0-4475-89d5-f30700320afe fwd="157.48.225.94" dyno=web.1 connect=0ms service=10ms status=304 bytes=270 protocol=https
2021-12-06T09:52:21.557993+00:00 heroku[router]: at=info method=GET path="/manifest.json" host=shop-spot.herokuapp.com request_id=a8cb8aee-8146-4df0-899f-d812227d1bfb fwd="157.48.225.94" dyno=web.1 connect=0ms service=5ms status=304 bytes=269 protocol=https
2021-12-06T09:52:21.871712+00:00 heroku[router]: at=info method=GET path="/logo192.png" host=shop-spot.herokuapp.com request_id=ff819f69-c546-4b40-9344-545f2377f7d2 fwd="157.48.225.94" dyno=web.1 connect=0ms service=1ms status=304 bytes=270 protocol=https
2021-12-06T09:52:22.353954+00:00 heroku[router]: at=info method=GET path="/manifest.json" host=shop-spot.herokuapp.com request_id=dde45574-f317-4771-b057-ea66804efbb6 fwd="157.48.225.94" dyno=web.1 connect=0ms service=1ms status=304 bytes=269 protocol=https
2021-12-06T09:52:22.670464+00:00 heroku[router]: at=info method=GET path="/logo192.png" host=shop-spot.herokuapp.com request_id=0a75d338-4f23-4cbc-bc31-5d0ced8bd8a8 fwd="157.48.225.94" dyno=web.1 connect=0ms service=1ms status=304 bytes=270 protocol=https
2021-12-06T10:06:21.000000+00:00 app[api]: Build started by user srinivasmoparthi17@gmail.com
2021-12-06T10:07:07.033792+00:00 app[api]: Release v38 created by user srinivasmoparthi17@gmail.com
2021-12-06T10:07:07.033792+00:00 app[api]: Deploy 79228b19 by user srinivasmoparthi17@gmail.com
2021-12-06T10:07:07.300298+00:00 heroku[web.1]: Restarting
2021-12-06T10:07:07.315781+00:00 heroku[web.1]: State changed from up to starting
2021-12-06T10:07:08.370421+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-12-06T10:07:08.613075+00:00 heroku[web.1]: Process exited with status 143
2021-12-06T10:07:09.000000+00:00 app[api]: Build succeeded
2021-12-06T10:07:12.863949+00:00 heroku[web.1]: Starting process with command `npm start`
2021-12-06T10:07:14.995361+00:00 app[web.1]:
2021-12-06T10:07:14.995376+00:00 app[web.1]: > projbackend@1.0.0 start /app
2021-12-06T10:07:14.995377+00:00 app[web.1]: > nodemon app.js
2021-12-06T10:07:14.995377+00:00 app[web.1]:
2021-12-06T10:07:15.539554+00:00 app[web.1]: [nodemon] 1.19.4
2021-12-06T10:07:15.540385+00:00 app[web.1]: [nodemon] to restart at any time, enter `rs`
2021-12-06T10:07:15.541878+00:00 app[web.1]: [nodemon] watching dir(s): *.*
2021-12-06T10:07:15.543473+00:00 app[web.1]: [nodemon] watching extensions: js,mjs,json
2021-12-06T10:07:15.543944+00:00 app[web.1]: [nodemon] starting `node app.js`
2021-12-06T10:07:17.214186+00:00 app[web.1]: App is running at 7000
2021-12-06T10:07:17.326823+00:00 heroku[web.1]: State changed from starting to up
2021-12-06T10:07:17.533441+00:00 app[web.1]: DB CONNECTED
在我部署此应用程序的本地机器上一切正常,但在其他设备上遇到问题。 请帮我找出这个原因的错误。提前谢谢你!
【问题讨论】:
Cors 可能会导致此问题。您可以删除所有 Cors 行并重试吗? @Abdulhakim 收到此错误 从源“shop-spot.herokuapp.com”获取“localhost:7000/api/products”的访问权限已被 CORS 策略阻止:没有“Access-Control-Allow-Origin”标头存在于请求的资源上。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。 即使在我的本地机器上,但在 localhost 上工作正常 【参考方案1】:我想通了,将 heroku postbuild 脚本添加到我的主 package.json 文件后,我的问题得到了解决。
【讨论】:
以上是关于为啥即使我的数据库已连接,我的 Heroku 应用程序在其他设备中打开时也没有加载任何数据?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 Django 应用程序可以在本地运行,但不能在 Heroku 上运行?