我构建了我的第一个MERN应用程序,一切都在我的本地开发环境中工作,但当我在Heroku上部署我的ExpressJS REST API时遇到了一些问题。部署完成后,大多数发送到后台的GET/POST/PATCH请求都可以正常工作。但是,我有一个POST和一个补丁请求,其中包括请求中返回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文件系统不允许永久存储,亚马逊S3将是一个很好的选择。我只是想知道是否仍然可以将临时图片上传作为第一步,或者我是否应该直接在亚马逊S3上实现存储?
编辑2:我今天又尝试了一件事-我在Heroku Node.js deploy故障排除页面上看到,本地和生产环境之间的节点和npm版本可能存在不匹配。因此,我在Heroku上检查了我的最新构建日志,并注意到我的节点和npm版本高于我的本地环境。我尝试通过添加一个"engines“部分来编辑我的package.json文件,在那里我可以指定我在本地环境中拥有的node和npm的确切版本。然而,仍然没有运气,因为上传图像的请求仍然失败。
发布于 2021-02-07 03:00:22
我能够找出我的错误并解决我自己的问题。
我从这个问题的顶部答案中得到了一个想法:uploading image to heroku using node and multer not work
答案是,如果multer上传到的本地“图像”文件夹为空,Heroku将不会在您部署时在服务器上创建该文件夹。在我的例子中,当我部署的时候,我已经在那个文件夹中有一些来自测试的图片--问题是我在我的.gitignore文件中有我的‘图像’文件夹路径。我这样做是因为这个项目在我的个人GitHub上,而我不想签入这些图片。我使用Git在Heroku上进行部署,通过在.gitignore中添加图像文件夹,我的图像文件夹不会包含在我的部署构建中。从.gitignore中删除它可以让我的图像上传请求正常工作。
https://stackoverflow.com/questions/66037248
复制相似问题