首页 > 解决方案 > Node-express-Typescript:邮递员不断发送帖子请求

问题描述

我正在为我的应用程序使用节点打字稿。我使用邮递员提出了基本的发布请求。我控制台记录请求正文。我得到了数据,但在邮递员中不断显示发送请求和图像。我在我的代码中没有看到任何错误。我不知道发布请求中有什么问题。附言: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.jstypescriptexpresspostpostman

解决方案


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

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()
})

如果你想发送任何响应,你可以将它传递给发送函数


推荐阅读