Losing POST data when sending HTTP status codes in Express 3.4.8

I have implemented a route handler in Express 3.4.8 to check for privileges on all my routes. In case the credentials sent with each request are invalid, I want to respond with a status of 403.

app.all('*',function(req,res,next){

  //req.body contains an object `credentials`

  res.send(403);
});

However, when I send a response with a status of 403, the req.body object becomes empty. This is strange because the request is coming from a correct Angular app.

If I remove the status code from the response, everything works fine. Here is the modified code:

app.all('*',function(req,res,next){
  res.send('Im not a status code');
});

When I use this code, req.body contains the expected credentials. It seems like the issue only occurs when sending a 403 status code.

Has anyone experienced this issue and found a solution?


This problem is causing a lot of frustration for me. It doesn't make sense at all. Even when I log req.body at the beginning of the logic flow, before the authentication check, it still fails.

Answer №1

Although I couldn't figure out why it failed in such a specific manner, I managed to find a solution to the problem.

The issue stemmed from CORS triggering Angular to send an OPTIONS request.

Check out this resource for more information: AngularJS performs an OPTIONS HTTP request for a cross-origin resource

To resolve this, I simply responded with a status 200 message for each OPTIONS request, allowing the original POST request to go through successfully.

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

When utilizing Ionic Firebase, the user profile saved in the sidemenu is stored using the Events class. However, the saved profile disappears as soon as

Hello there. I am currently working on displaying my user's information in the sidemenu, and after some research, I found that using the Events class might solve this issue. However, I have noticed that the data saved in subscribe gets destroyed whene ...

Utilizing React in Express: A Step-by-Step Guide

I have a website built with expressjs/nodejs. I am looking to integrate a reactjs module into it. Currently, I have added a build of the reactjs app to 'client/build' within my expressjs application. The static declaration for 'client' ...

Organizing seating arrangements for a concert hall performance

My goal is to develop a custom concert seat booking system using HTML, CSS, and JavaScript. For example, if the concert venue has 10 rows with 20 seats in each row, I could organize them like this: const rows = [ { name: 'A', seats: [1, 2, 3, ...

The React app I've been working on has a tendency to unexpectedly crash when reloading the code from time

Dealing with a frustrating issue here. I've been working on an app and for the past few weeks, it keeps crashing unexpectedly. It seems to happen more frequently after saving my code to trigger a reload, but it can also just crash randomly while navig ...

Utilize JQuery variables for toggling the visibility of various DIV elements

On my webpage's splash page, there are 4 divs but only the home div is initially visible. The other three are hidden. Each of these divs has a button associated with it that triggers a jquery click event to swap out the currently visible div for the ...

Delete a designated section from a URL with the power of jQuery

I have a URL like http://myurleg.com/ar/Message.html and I need to change ar to en in it when clicked on. For example, if my current URL is: http://myurleg.com/ar/Message.html After clicking, it should become: http://myurleg.com/en/Message.html I attemp ...

What could be the reason why the toggleClass function is not being run

I've been running into a little issue with using the .toggleClass function in my code. It seems to work inconsistently, and despite reading various posts on the topic, I haven't found a solution that works for me. Could you provide some assistan ...

Incorporate new content into a jquery mobile listview on the fly

I'm attempting to use JavaScript to dynamically populate a listview with items. Here is the code I have written: //Function to load data fields for items dynamically function initialiseFields(listViewId){ for(var i = 0; i < 10; i++){ ...

Inaccurate Feedback on Maquest Direction Route API

Currently, I am in the process of implementing the MapQuest API Direction Routing function on my website. However, upon submitting a request to the API, it is returning inaccurate routing points between two destinations. Here is the request form that I am ...

The React application functions smoothly when executed using react-scripts start, but encounters an "Unexpected SyntaxError: Unexpected Token: <" error upon building

My goal is to deploy my portfolio site using an express server and react-scripts build. Everything works perfectly fine when I run react-scripts start. However, when I try to serve up the build index.js, I encounter the following errors: 2.8b4a0c83.chunk.j ...

Tips for responding to a chat conversation via email

Looking to implement a feature where when one user initiates a conversation in the chat, an email is automatically sent out to notify other chat users. The recipient can then reply directly from their email and have it added to the chat conversation. I a ...

Angular2: PrimeNG - Error Retrieving Data (404 Not Found)

I'm facing an issue with using Dialog from the PrimeNG Module. The error message I keep getting is: Unhandled Promise rejection: (SystemJS) Error: XHR error (404 Not Found) loading http://localhost:4200/node_modules/primeng/primeng.js I followed the ...

Contrasting gatsby-theme-material-ui and gatsby-plugin-material-ui

I'm in the process of creating a website using Gatsby, and I'd like to incorporate Material UI, but I'm unsure about which plugin I should use. Here are my questions: What is the difference between these two options, and how can I integra ...

What is the best way to specify when form inputs are required in AngularJS?

Imagine developing a contact management application (for demonstration purposes) utilizing AngularJS. We have crafted a contact form that includes fields for email and phone number. We aim to make one of the two mandatory, but not both: The email field sh ...

Retrieving External JSON Data from a Server with Firefox

I've been developing a static local HTML5 charting application that retrieves data from a remote server for output. The code works perfectly in Google Chrome, as shown below, but I'm encountering difficulties getting it to function in Firefox. & ...

Employing an angular ng-model expression

Inside a controller: $scope.bar = {'label': 'user_label', 'bindTo': 'user.name'}; Next, in the HTML: <label for="user_label">User Label</label> <input name="{{bar.label}}" type="text" class="form-co ...

Ways to track down an ajax error response?

Whenever I encounter an AJAX error response with jQuery, the following log is displayed: jquery.js:8630 OPTIONS http://10.0.1.108:8000/api/v1.0/auth/ net::ERR_CONNECTION_REFUSED Object {readyState: 0, status: 0, statusText: "error"} Within my object, the ...

Can JQuery be used to identify the CSS styles applied to selected elements?

Highlight the text by selecting it and clicking a button. If the text is already highlighted, clicking the button should make the selected text return to normal. Can this be achieved using jQuery and a single button? Is it possible to identify the CSS st ...

Is it possible to utilize lambda:invoke to trigger an AWS Lambda express application?

Looking for a solution for my AWS Lambda express app that has multiple endpoints. Is there a way to bypass api-gateway and invoke the app directly using lambda:invoke? I'm struggling to find out how to access specific endpoints. Any suggestions or g ...

The tab indicator in Material-UI fails to update when the back button is clicked

My code is currently functioning well: The tab indicator moves according to the URL of my tab. However, there is a peculiar issue that arises when the back button of the browser is pressed - the URL changes but the indicator remains on the same tab as befo ...