ExpressJS API部署在Heroku给出500内部服务器错误



我构建了我的第一个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上的最新构建日志,注意到我的node和npm版本高于我的本地环境。我试着编辑我的包。通过添加"引擎"节中,我可以指定本地环境中node和npm的确切版本。然而,仍然没有运气,因为上传图像的请求仍然失败。

我发现了我的错误并解决了我自己的问题。

我从这个问题的第一个答案得到了一个想法:使用节点和multer上传图像到heroku不工作

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

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

最新更新