Reputation: 4638
I'm trying to emit a simple POST request using Postman to my express app, however the post request is timing out.
Here is the request:
And here is the app:
import express from 'express'
import bodyParser from 'body-parser'
import path from 'path'
const app = express()
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/', (req,res) => {
console.log(req.body)
if (req.body.inviteCode === "12") {
res.json({value: "success"})
} else {
res.json({value: "fail"})
}
})
app.listen(process.env.PORT || 3000, () => {
console.log(`App listening on ${process.env.PORT || 3000}`)
})
console.log(req.body)
prints out an empty object {}
Any idea what could be going wrong?
Upvotes: 1
Views: 639
Reputation: 1156
You are not including inviteCode
in the POST parameters in Postman (as seen in your screenshot, the key-value entry under the "Body" tab), instead you are passing inviteCode
as a URL query parameter, which will be set in req.query.inviteCode
.
To use it from the query parameter (in the URL), matching your Postman request:
app.post('/', (req,res) => {
req.query.inviteCode === "99" ? res.json({value: "success"}) : res.json({value: "fail"})
})
Or to use it in the POST body, which requires fixing your Postman request:
app.post('/', (req,res) => {
console.log(req.body)
req.body.inviteCode === "99" ? res.json({value: "success"}) : res.json({value: "fail"})
})
Upvotes: 1