部署在 Heroku 上的 ExpressJS API 出现 500 内部服务器错误

ExpressJS API Deployed on Heroku Gives 500 Internal Server Error

我构建了我的第一个 MERN 应用程序,一切都在我的本地开发环境中运行,但是 运行 在 Heroku 上部署我的 ExpressJS REST API 时遇到了一些问题。部署后,大多数 GET/POST/PATCH 发送到后端的请求都正常工作。但是,我有一个 POST 和一个 PATCH 请求,其中包含返回 500 内部服务器错误的请求中的图像。对于这些请求,我使用的是 Axios,图像作为 FormData.

附加到请求中

以下是我用命令heroku logs -t得到的日志:

2021-02-03T22:00:04.722403+00:00 app[web.1]: RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: ENOENT
2021-02-03T22:00:04.722443+00:00 app[web.1]: at ServerResponse.writeHead (_http_server.js:248:11)
2021-02-03T22:00:04.722444+00:00 app[web.1]: at ServerResponse._implicitHeader (_http_server.js:239:8)
2021-02-03T22:00:04.722445+00:00 app[web.1]: at write_ (_http_outgoing.js:654:9)
2021-02-03T22:00:04.722445+00:00 app[web.1]: at ServerResponse.end (_http_outgoing.js:766:5)
2021-02-03T22:00:04.722446+00:00 app[web.1]: at ServerResponse.send (/app/node_modules/express/lib/response.js:221:10)
2021-02-03T22:00:04.722446+00:00 app[web.1]: at ServerResponse.json (/app/node_modules/express/lib/response.js:267:15)
2021-02-03T22:00:04.722448+00:00 app[web.1]: at /app/app.js:51:4
2021-02-03T22:00:04.722448+00:00 app[web.1]: at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:71:5)
2021-02-03T22:00:04.722449+00:00 app[web.1]: at trim_prefix (/app/node_modules/express/lib/router/index.js:315:13)
2021-02-03T22:00:04.722449+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:284:7
2021-02-03T22:00:04.722450+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
2021-02-03T22:00:04.722450+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:275:10)
2021-02-03T22:00:04.722451+00:00 app[web.1]: at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:67:12)
2021-02-03T22:00:04.722451+00:00 app[web.1]: at trim_prefix (/app/node_modules/express/lib/router/index.js:315:13)
2021-02-03T22:00:04.722451+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:284:7
2021-02-03T22:00:04.722452+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
2021-02-03T22:00:04.724981+00:00 heroku[router]: at=info method=PATCH path="/api/user/profilePic/601721519ab4390015d2439a" host=mycookbook-backend.herokuapp.com request_id=6d1bb670-2c07-4078-833a-4ae3ae383be2 fwd="99.229.62.182" dyno=web.1 connect=1ms service=20ms status=500 bytes=627 protocol=https

我还尝试在前端收到后端响应时在控制台中打印 err.response 对象:

err.response: {
    "data":
        "<!DOCTYPE html>
            <html lang=\"en\">
                <head>
                    <meta charset=\"utf-8\">
                    <title>Error</title>
                </head>
                <body>
                    <pre>Internal Server Error</pre>
                </body>
            </html>",
    "status":500,
    "statusText":"Internal Server Error",
    "headers":{
        "content-length":"148",
        "content-type":"text/html; charset=utf-8"
        },
    "config":{
        "url":"/user/profilePic/userId",
        "method":"patch",
        "data":{},
        "headers":{
            "Accept":"application/json, text/plain, */*",
            "Authorization":"Bearer authToken"
            },
        "baseURL":"https://mycookbook-backend.herokuapp.com/api/",
        "transformRequest":[null],
        "transformResponse":[null],
        "timeout":0,
        "xsrfCookieName":"XSRF-TOKEN",
        "xsrfHeaderName":"X-XSRF-TOKEN",
        "maxContentLength":-1,
        "maxBodyLength":-1
        },
    "request":{}
    }

在我的 MongoDB 数据库中,我确保授予对 0.0.0.0/0 的网络访问权限,以便 Heroku 上的后端可以访问它。我还在 Heroku 上仔细检查了我的配置变量,这些应该是正确的,因为其他请求工作正常。非常感谢任何解决此问题的提示或指导。

编辑: 我忘了说我正在使用 multer 来上传图片。我还知道 Heroku 文件系统不允许永久存储,而 Amazon S3 将是一个很好的替代方案。我只是想知道临时图像上传是否仍然可以作为第一步,还是我应该直接在 Amazon S3 上实施存储?

编辑 2:我今天尝试的另一件事 - 我在 Heroku Node.js 部署故障排除页面上读到,本地和生产环境之间的节点和 npm 版本可能不匹配。所以我检查了我在 Heroku 上的最新构建日志,发现我的节点和 npm 版本高于我的本地环境。我尝试通过添加一个“引擎”部分来编辑我的 package.json 文件,我可以在其中指定我在本地环境中拥有的节点和 npm 的确切版本。然而,仍然不走运,因为上传图片的请求仍然失败。

我能够找出我的错误并解决我自己的问题。

我从这个问题的最佳答案中得到了一个想法: uploading image to heroku using node and multer not work

答案是,如果 multer 上传到的本地 'images' 文件夹是空的,Heroku 将不会在您部署时在服务器上创建该文件夹。就我而言,部署时该文件夹中已经有一些来自测试的图像 - 问题是我的 'images' 文件夹路径在我的 .gitignore 文件中。我这样做是因为这个项目在我的个人 GitHub 上,我不想签入这些图像。我使用 Git 在 Heroku 上部署,通过在 .gitignore 中添加图像文件夹,我的图像文件夹未包含在我的部署构建中。从 .gitignore 中删除它允许我的图片上传请求工作。

Git 不会跟踪空文件夹,因此请确保您已手动将一些虚拟数据添加到应用程序使用的任何空文件夹中。例如 multer 使用的那些。这可以防止 POST、Heroku 上的 PATCH 和 PUT 请求错误。