Node-express-Typescript: Postman keep sending post request

Viewed 354

I am using node-typescript for my app. I made basic post request by using post man. I console log the request body. I get the data but in postman keep showing sending request and image. I don't see anything wrong in my code. I don't know what is the problem in post request. Ps: This is my first time I am using node-typescript express server.

This is basically all my express code

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

This is my command script

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

You are not ending any your request. You need to send response once you request gets processed.

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

Update above block as follow,

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

If you want to send anything in response, you can just pass it to send function

Related