gpt4 book ai didi

javascript - 将应用程序部署到 Heroku 会引发错误 { path =“/” path =“/favicon.ico” }。但是在本地主机上运行

转载 作者:行者123 更新时间:2023-12-03 12:15:04 25 4
gpt4 key购买 nike

我正在尝试将我的应用程序部署到 Heroku。即使它在本地主机上运行,​​我也收到 503(服务不可用)错误。我尝试了很多解决方案,但都没有奏效。我的 app.js 文件

if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}

const express = require("express");
const mongoose = require("mongoose");
const { ApolloServer } = require("apollo-server-express");
const auth = require("./middleware/auth");
const userController = require("./controllers/userController");
const typeDefs = require("./schema");
const resolvers = require("./resolvers");

const port = process.env.PORT || 4000;
const app = express();
app.set("port", port);

app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"OPTIONS, GET, POST, PUT, PATCH, DELETE"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
next();
});

app.use(auth);
app.get("/email-confirmation/:token", userController.confirmationPost);

const server = new ApolloServer({
typeDefs,
resolvers,
formatError: (err) => {
if (!err.originalError) {
return err;
}
if (err.message.startsWith("Database Error: ")) {
err.message = "Internal server error";
}
const data = err.originalError.data;
const message = err.message || "Internal server error.";
const code = err.originalError.code || 500;
return { message: message, status: code, data: data };
},
context: ({ req, res }) => ({
req,
res,
}),
});

server.applyMiddleware({ app });

mongoose
.connect(process.env.DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(() => {
console.log("conneted to database");
app.listen(port, () => {
console.log("listening for requests on port " + port);
});
});

还有我的 package.json 是:

{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js",
"dev": "nodemon app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"apollo-server-express": "^2.17.0",
"bcryptjs": "^2.4.3",
"crypto": "^1.0.1",
"express": "^4.17.1",
"graphql": "^15.3.0",
"jsonwebtoken": "^8.3.0",
"mongoose": "^5.10.2",
"nodemailer": "^6.4.16",
"nodemailer-sendgrid-transport": "^0.2.0",
"validator": "^10.8.0"
},
"devDependencies": {
"dotenv": "^8.2.0",
"eslint": "^7.7.0",
"nodemon": "^2.0.6"
}
}

heroku 日志 --tail 命令给出以下输出:
screenshot
我已经尝试了所有解决方案。但他们似乎都没有解决这个问题。请帮忙。
更新:
在 Heroku 中设置 DB_URL 后,它开始工作,但出现另一个错误。 screenshot2
安慰:

1. GET https://capstone-ecommerce-backend.herokuapp.com/ 404 (Not Found)
2. Refused to load the image 'https://capstone-ecommerce-backend.herokuapp.com/favicon.ico' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.

最佳答案

尝试设置 DB_URL在您的 Heroku 应用程序中。使用以下命令:

$ heroku config:set DB_URL=database_uri_here

关于javascript - 将应用程序部署到 Heroku 会引发错误 { path =“/” path =“/favicon.ico” }。但是在本地主机上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65275827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com