【发布时间】:2021-02-03 23:27:56
【问题描述】:
我构建了我的第一个 MERN 应用程序,并且一切都在我的本地开发环境中运行,但是当我在 Heroku 上部署我的 ExpressJS REST API 时遇到了一些问题。部署后,发送到后端的大多数 GET/POST/PATCH 请求都可以正常工作。但是,我有一个 POST 和一个 PATCH 请求,其中包括请求中返回 500 Internal Server Error 的图像。对于这些请求,我使用的是 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 上的最新构建日志,发现我的 node 和 npm 版本高于我的本地环境。我尝试通过添加一个“引擎”部分来编辑我的 package.json 文件,我可以在其中指定我在本地环境中拥有的节点和 npm 的确切版本。但是,仍然没有运气,因为上传图片的请求仍然失败。
【问题讨论】:
标签: reactjs mongodb express heroku multer