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

时间:2021-02-03 23:27:56

标签: reactjs mongodb express heroku multer

我构建了我的第一个 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 部署故障排除页面上读到,本地和生产环境之间的 node 和 npm 版本可能不匹配。所以我在 Heroku 上查看了我最新的构建日志,发现我的 node 和 npm 版本高于我的本地环境。我尝试通过添加一个“引擎”部分来编辑我的 package.json 文件,我可以在其中指定我在本地环境中拥有的节点和 npm 的确切版本。但是,仍然没有运气,因为上传图片的请求仍然失败。

1 个答案:

答案 0 :(得分:0)

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

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

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