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
 |  Show 2 more ments

2 Answers 2

Reset to default 0

Actually you code is running in my machine, maybe you just forgot some postman configuration.

  1. content-type: application/json
  2. method: post
  3. 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