简体   繁体   English

向我的快递服务器发送请求返回错误

[英]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. 我刚刚开始使用node和express,并试图发出一个发布请求并将结果立即推送到一个数组。 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 内容类型:application / json
  2. method: post 方法:发布
  3. row data: {"data": "test"} 行数据:{“数据”:“测试”}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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