【问题标题】:Node-express-Typescript: Postman keep sending post requestNode-express-Typescript:邮递员不断发送帖子请求
【发布时间】:2021-05-23 18:39:18
【问题描述】:

我正在为我的应用程序使用节点打字稿。我使用邮递员提出了基本的发布请求。我控制台记录请求正文。我得到了数据,但在邮递员中不断显示发送请求和image。我在我的代码中没有看到任何错误。我不知道发布请求中有什么问题。 ps:This is my first time I am using node-typescript express server.

这基本上是我所有的快递代码

import express, { Application, Response, Request } from 'express';
import cors from 'cors';
import morgan from 'morgan';
import helmet from 'helmet';


const app: Application = express();
const port = 8000;


app.use(cors());
app.use(morgan("common"));
app.use(helmet());
app.use(express.json());


app.post('/api', (req: Request, res: Response) => {

   console.log(req.body.Item); // I get the data

 
})

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`)
})

这是我的命令脚本

  "scripts": {
    "build": "tsc -p .",
    "start": "ts-node server.ts",
    "server": "ts-node server.ts"
  },

【问题讨论】:

    标签: node.js typescript express post postman


    【解决方案1】:

    您没有结束任何请求。一旦您的请求得到处理,您需要发送响应。

    app.post('/api', (req: Request, res: Response) => {
       console.log(req.body.Item); // I get the data
    })
    

    更新上面的块如下,

    app.post('/api', (req: Request, res: Response) => {
       console.log(req.body.Item); // I get the data
       res.send()
    })
    

    如果您想发送任何响应,只需将其传递给 send 函数

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 2019-04-26
      • 1970-01-01
      • 2021-11-07
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      相关资源
      最近更新 更多