Ways to retrieve POST data in express js

When accessing a website in the browser, I use:

  xmlHttp.open("POST", "/", true); // true for asynchronous

  xmlHttp.send("data");

on the client side browser.

For node js application, I'm using:

app.post("/", function (req, res) {
  console.log("Got a POST request");
  console.log(req);
});

to receive post data. However, no matter what data I send, it doesn't display. It's frustrating that I have to install additional "body parser" package just to view data without parsing it. A post request is supposed to send data to the server... Where did the data go?

Answer №1

I refuse to add an additional "body parser" package just to view the data.... I have no interest in parsing it.

Oh well.

Even if you prefer to handle a simple text body, you still need to utilize body parsing middleware for that purpose.

One of the tasks of this middleware is to capture and store the incoming client data in memory (under req.body) instead of discarding it.

Keep in mind that Express.js automatically includes body parsing middleware as part of its dependencies.

Answer №2

Forget about using a standalone body parser library. Instead, leverage express.json, a conveniently available middleware function in Express. Implementation is as simple as:

app.use(express.json())

Dive deeper into its usage from this source.

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

Refreshing dynamically added rows through ajax updates

I'm struggling to clearly articulate my problem. Currently, I have a function that retrieves values from a database using ajax and then dynamically adds each result as a row in a table. This allows users to edit or delete any row as needed. I'm ...

Getting attribute values from custom tags in AngularJS is a common task that can be

I am new to AngularJS and I'm having trouble with my code. I am attempting to retrieve the attribute value of post-id from my index.html file and display it in the console from my controller. Here is a snippet from my index.html: <post-creator po ...

In the production mode, Webpack doesn't compile any code

I recently followed the typescript guide at https://webpack.js.org/guides/typescript/ After running webpack in "production" mode, I noticed that it emitted very minimal output. Below is the code from my src/index.ts file: export function foo() { return ...

Developing Vue applications with dynamic component insertion

Looking to develop a user-friendly form builder using Vue, where users can easily add different form fields by clicking buttons from a menu. If I only had one type of form field to add, it could be done like this (https://jsfiddle.net/u6j1uc3u/32/): <d ...

php script within a literal .tpl file

Is there a way to perform a json_encode within a literal javascript block in the context of a Smarty template? {literal} <script> function openWin() { var O = {php} echo json_encode($obj);{/php}; // syntax error ...

Window backdrop being filled

I am attempting to set a background image that fills the entire window regardless of its size. I have implemented html, css and script as shown below: // Function adaptImage() // Parameters: targetimg function adaptImage(targetimg) { var wheight = $ ...

Tips for sending transactions across multiple collections in Mongoose

Important Note: I establish a connection to the database using the following code: const mongoose = require('mongoose'); const connectDB = (url) => { return mongoose.connect(url); } Issue Summary: I am faced with a situation where two diff ...

Trigger the reactive helper again when there is a change in the $scope

I am working with a reactive data source in Angular-Meteor and I want to find a method to trigger the reactive function to run again after modifying another $scope value. Specifically, I aim to change the sorting order based on $scope.model.sort: angular. ...

Is it possible to create a Nuxt.js static website that incorporates a dynamic single-page application built with Vue.js

Currently researching Static Site Generators for a client project, I discovered Vue.js and Nuxt.js, which appear promising. Given the choice between Vue.js and React, I lean towards Vue at the moment. The client's website consists of 120 pages with s ...

Creating dynamic select boxes in Django Admin using jQuery

In my Contract class, the contract_mod field is designed to extend a contract from a previous one and should only display contracts related to the selected person. The Contract class returns the person field, but since I have no experience with AJAX/jQuery ...

Reducing the size of textures in three.js

Is it possible to shrink the faces of a model rendered in the browser using Threejs, without affecting the mesh structure? I am looking for suggestions on how to accomplish this unique task. ...

Insert several elements into each array that are the same

$ node > A = [0, 1, 23] [ 0, 1, 23 ] > B = A [ 0, 1, 23 ] > A.splice(0, 3) [ 0, 1, 23 ] > B [] > A [] > A = A.concat([1, 2]) [ 1, 2 ] > B [] Indeed, the code execution sequence yields the expected results. However, do you think there ...

Creating custom back button functionality in Vue.js to mimic an app-like experience with routing

As I work on my cordova Vue app, I often encounter deeper links like this: /profile/:id/:contact_type/contacts:contact_id Usually, you would start on /profile/:id and then navigate to /profile/:id/:contact_type/contacts:contact_id by clicking a link. Th ...

Enhance a link using jQuery to allow it to expand and collapse

I am currently working on an MVC view that features a 3-column table showcasing a list of products. The first column, which contains the product names, is clickable and directs users to a specific product page. I am looking to implement functionality where ...

Update and send back variable on the spot

I'm interested in learning the syntax for creating an inline function that can accept a parameter, perform a simple modification on it, and return it all in a single line. Here's an example: (input) => { input.status = 'complete'; ...

Ensuring Angular applications can effectively run on Internet Explorer

In my Angular application, I have implemented the functionality where users can choose a map to select the delivery point for goods. However, there seems to be an issue with this feature in Internet Explorer (IE) - the map opens but the delivery points ar ...

Error message encountered upon initializing a node.js application with Socket.io

I am currently utilizing socket.io for internal communication within my node.js application. While the code functions properly on my development machine, a continuous stream of messages appears during app startup on the main server: GET /socket.io/?EIO=3& ...

Discover the secrets to replicating the mesmerizing horizontal scrolling menu akin to the

What is the most effective way to create a horizontal menu similar to the innovative Google picture menu? Could someone kindly share their knowledge and provide the code for achieving the same outcome? ...

What is the process for transferring a PDF document to the frontend?

I have a file saved in .PDF format on my computer, and I am attempting to send it to the frontend using node/express. Although I am able to successfully send the file as a binary string stream to the frontend, when trying to download the .PDF onto the use ...

Execute JavaScript function with a delay once the webpage has finished loading

I am currently working on a basic function that runs when the page is loaded. However, even though the page has finished loading, there are times when it takes about half a second for the content to actually appear. While this typically wouldn't be an ...