简体   繁体   中英

Post request to my express server returns an error

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

在此处输入图片说明

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"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM