The Express app.post endpoint is not acknowledging incoming POST requests from Postman

I am facing an issue where my POST request using Postman to my express app 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) shows an empty object {}

Any thoughts on what could be causing this issue?

Answer №1

Ensure that you are not omitting the inviteCode from the POST parameters in Postman, as noted in your screenshot where it should be included under the "Body" tab. Instead, you seem to be passing the inviteCode as a URL query parameter, which will then be accessed using req.query.inviteCode.

To make use of the query parameter (in the URL) in line with your Postman request:

app.post('/', (req,res) => {
  req.query.inviteCode === "99" ? res.json({value: "success"}) : res.json({value: "fail"})
})

Alternatively, if you want to utilize the inviteCode in the POST body, you'll need to correct your Postman request:

app.post('/', (req,res) => {
  console.log(req.body)

  req.body.inviteCode === "99" ? res.json({value: "success"}) : res.json({value: "fail"})
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the formula for determining the size of an image using JavaScript, jQuery, or Java?

I'm looking to determine the dimensions of an image. I have both the image src and base64 data available. My goal is to display the current size of the image. I am working with Java and JavaScript. Is there a way for me to calculate the image size usi ...

Display a helpful tooltip when hovering over elements with the use of d3-tip.js

Is it possible to display a tooltip when hovering over existing SVG elements? In this example, the elements are created during data binding. However, in my case, the circles already exist in the DOM and I need to select them right after selectedElms.enter ...

Is there a way for me to update the button text and class to make it toggle-like

Is there a way to switch the button text and class when toggling? Currently, the default settings show a blue button with "Show" text, but upon click it should change to "Hide" with a white button background. <button class="btn exam-int-btn">Show< ...

After changing the page, the Facebook JS SDK fails to function properly when using JQueryMobile

I'm facing an issue with my webapp that utilizes jQuery Mobile for full ajax navigation. I have initialized the Facebook SDK at the pageinit event in jQueryMobile (called on each page). jQuery(document).on('pageinit', function (event) { ...

getting rid of the angular hash symbol and prefix from the anchor tag

I am having difficulty creating a direct anchor link to a website. Whenever I attempt to link to the ID using: where #20841 is my anchor tag. Angular interferes with the URL and changes it to: This functions properly in Chrome and Firefox, but i ...

Using a function as an argument within an Angular directive

Looking for a solution to pass a promise-returning function into a directive? Here's what I'm currently doing: In the parent controller, I've created a callback: $scope.myCb = function(data) { console.log(data); } Directive Scope: sco ...

Continuously incorporating Ajax requests into a queue

I'm currently tackling a new feature at work and find myself at a crucial point. Despite being unfamiliar with Ajax, I am determined to set up a manual queue to handle the requests instead of relying on the browser. After some research and referring t ...

Mastering the Implementation of Timetable.js in Angular with TypeScript

I am currently working on integrating an amazing JavaScript plugin called Timetable.js into my Angular6 project. You can find the plugin here and its repository on Github here. While searching for a way to implement this plugin, I stumbled upon a helpful ...

Trouble connecting Local Node.js script with mongoDB database

After setting up a MongoDB database on MongoLab through Heroku, I successfully connected to it via the shell using this command: mongo ds061701.mongolab.com:61701/heroku_app35721468 -u <dbuser> -p <dbpassword> In my Node.js and Express projec ...

Utilizing JavaScript within the Spring MVC form tag

Can you please assist me with the following question? I am currently working on a project using JSP, where we are utilizing Spring form tags. My issue involves creating two radio buttons that, when clicked, will display different options and pass the sele ...

What is the method to run a script within a particular div?

Here is an example of the DOM structure: <div> <button>Click me</button> <img src=#> </div> <div> <button>Click me</button> <img src=#> </div> <div> <button>Clic ...

"Put Jest to the test by running it with the Express

Currently, I am in the process of learning how to build an API with TypeScript and experimenting with testing it using the Jest framework. When utilizing a basic Express application like app = express() supertest(app) everything works smoothly. However, ...

Exploring the intricacies of logic through the interactive features of .hover and .click in tandem with Raphael

My goal is to create a hover effect and a click state for a specific area on a map. At present, I want one color to appear when hovering, and another color to display when clicked. Ideally, I'd like the click color to remain even after the user moves ...

AJAX response from open cart is returning null JSON data

Hey there, I'm having a problem with my ajax request. It seems like I keep receiving a null value for json. Let me show you the JavaScript code... <script> $(document).ready( function() { $('#donate-box-submit').on('click' ...

The onblur event is triggering prior to the value being updated

There are two input fields within a <form> element. I am trying to retrieve the value of one input field (dpFin) once it has been changed. The issue is that when I attempt to get the new value inside the event using var endDt = document.getElementByI ...

Divide text to reduce its width within the confines of a specific height on a div element

I've spent the past week scouring various forums, including stackoverflow, but I haven't been able to find a solution. In my responsive website, I'm using CSS flexbox to display dynamic menu items. Sometimes the text can be quite long, and ...

An error message of "TypeError: Contact is not a constructor" is displayed in Postman when attempting to validate JSON data for a post

After successfully running node app.js without any errors, I encountered an issue when trying to route localhost:3000/api/contact and post JSON data using Postman. Despite looking at similar questions, I am still unable to resolve the problem. Below is a s ...

The post() method in Express JS is functioning flawlessly in Firebase cloud function after deployment, however, it seems to encounter issues when running on a

https://i.stack.imgur.com/bIbOD.pngI am facing an issue with my Express JS application. Despite having both post() and get() requests, the post() request is not working on my local machine. It keeps throwing a 404 error with the message "Cannot POST / ...

EJS fails to display the EJS document

I am working on developing a queue system to generate PDF files. To accomplish this, I have set up a node server using the Express framework and integrated rabbitmq for the queue system. Additionally, I have configured the view engine as ejs. app.set(&apos ...

What is the best way to share dynamic content from a DIV or SPAN element on WhatsApp using jQuery?

I’ve been attempting to share the text content of a specific DIV on WhatsApp, but so far I haven't been successful. My goal is to only share a portion of the document (specifically the contents of showques). Below is the code snippet I've been ...