admin管理员组文章数量:1410730
Im just getting started with node and express and im trying to make a post request and push the results to an array for now. In the future i will be making these requests to a database, but for learning purposes i am doing this to an array now. Getting responses works, post requests gives me an error in postman.
const express = require('express');
const app = express();
const port = 5000;
const arr = [];
app.use(express.json());
app.post('/' , (req,res) => {
arr.push(req.body);
res.send(arr);
})
app.listen(port, () => {console.log(`listening on port ${port}`)});
Sadly the postman error doesnt tell me much.
The error :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /</pre>
</body>
</html>
The request :
{
"app" : "Post"
}
Specifications of requeSt :
connection →keep-alive
content-length →140
content-security-policy →default-src 'none'
content-type →text/html; charset=utf-8
date →Fri, 23 Aug 2019 07:55:32 GMT
x-content-type-options →nosniff
x-powered-by →Express
Im just getting started with node and express and im trying to make a post request and push the results to an array for now. In the future i will be making these requests to a database, but for learning purposes i am doing this to an array now. Getting responses works, post requests gives me an error in postman.
const express = require('express');
const app = express();
const port = 5000;
const arr = [];
app.use(express.json());
app.post('/' , (req,res) => {
arr.push(req.body);
res.send(arr);
})
app.listen(port, () => {console.log(`listening on port ${port}`)});
Sadly the postman error doesnt tell me much.
The error :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /</pre>
</body>
</html>
The request :
{
"app" : "Post"
}
Specifications of requeSt :
connection →keep-alive
content-length →140
content-security-policy →default-src 'none'
content-type →text/html; charset=utf-8
date →Fri, 23 Aug 2019 07:55:32 GMT
x-content-type-options →nosniff
x-powered-by →Express
Share
Improve this question
edited Aug 23, 2019 at 7:56
Kevin.a
asked Aug 23, 2019 at 7:51
Kevin.aKevin.a
4,32615 gold badges51 silver badges92 bronze badges
7
- Can you provide the error and maybe the request you're doing? – ThomasThiebaud Commented Aug 23, 2019 at 7:52
- could you please post the error – sid Commented Aug 23, 2019 at 7:53
- @PimentoWeb — Why? It's not being reassigned anywhere. – Quentin Commented Aug 23, 2019 at 7:55
- @PimentoWeb — The value is not being changed. It is always a reference to the same array. (The array being mutated is by-the-by) – Quentin Commented Aug 23, 2019 at 7:58
- It only changes once, im pushing to it. I can push to an array even if its a constant. If it was changing multiple times it would be a let. @PimentoWeb – Kevin.a Commented Aug 23, 2019 at 7:58
2 Answers
Reset to default 0Actually you code is running in my machine, maybe you just forgot some postman configuration.
- content-type: application/json
- method: post
- row data: {"data": "test"}
Another reason for this error could be attempting to use the 'get' method with a 'post' request in Postman. Therefore, you should ensure to select 'post' from the dropdown menu next to your API endpoint in Postman.
本文标签: javascriptPost request to my express server returns an errorStack Overflow
版权声明:本文标题:javascript - Post request to my express server returns an error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745029985a2638435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论