Obtain information from the get request route in Node.js

I've been diving into nodejs and databases with the help of an online resource.

As part of my learning process, I have been tasked with replicating the code below to fetch data from app.use('/server/profil');

However, I'm encountering a problem - the req.body from app.post is returning an empty object instead of the expected stuff object.

If anyone can offer assistance, it would be greatly appreciated.

Thank you in advance for any help provided.

    const express = require('express');

    const app = express()
    const cors = require('cors');
    app.use(cors());

    app.use(express.json());
    app.use(express.urlencoded({extended: true}));

    // This code sets a default path for public files at http://localhost:3000/public
    app.use(express.static('public'));

    app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content, Accept,         Content-Type, Authorization');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
    next();
    });

    app.post('/server/profil', (req, res, next) => {
    console.log(req.body);
    res.status(201).json({
    message: 'Thing created successfully!'
    });
    });

    app.use('/server/profil', (req, res, next) => {
    const stuff = [
    {
    _id: 'oeihfzeoi',
    title: 'My first thing',
    description: 'All of the info about my first thing',
    imageUrl: '',
    price: 4900,
    userId: 'qsomihvqios',
   },
   {
    _id: 'oeihfzeomoihi',
    title: 'My second thing',
    description: 'All of the info about my second thing',
    imageUrl: '',
    price: 2900,
    userId: 'qsomihvqios',
    },
    ];
    res.status(200).json(stuff);
    res.send(stuff);
    });

    module.exports = app;

Answer №1

To verify the api using Postman, it is necessary to configure your request. You may need to utilize a query string or specify the type of body request. (Refer to the image linked below) https://i.stack.imgur.com/ktVuk.png

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

How come this isn't growing the way I had hoped for?

Currently, I am working on a small jQuery script that is designed to fetch and print a specific month from a PHP file. Here is my code snippet: var count = 0; $(".nextMonth").click( function(event) { event.preventDefault(); count++; $("#r ...

Is it possible to call a ref from a different component in React?

I'm currently working on a React chat application and I want the input field where messages are entered to be focused every time you click on the chat box. However, the challenge I'm facing is that the chat box in the main component is separate ...

The NodeJS Express Docker container shut down successfully with code 0

Objective I am facing an issue with my Docker Compose setup. I have 3 instances defined in my `dockercompose.yml`, but I am only able to start 2 of them successfully. The third instance, which is running a Node Express server, seems to be causing problems ...

Ways to display the modal once the user initiates the action

Is there a way to delay loading my modal HTML codes until after the user clicks a button, rather than having them load automatically with the template? HTML <!-- Template Codes--> <button data-toggle="modal" data-target="#modal-content" type="bu ...

Stagnant className in map persisting despite changes being made

I am in the process of updating my react className based on changes to the active status within the sites variable, which is iterated over with a map function. The issue I'm facing is that the 'inactive' className persists even when the act ...

Error: The function props.addToCart is not accessible

While attempting to trigger my action on the client's click of the "addToCart" button to add a new product to the cart, I encountered the error message: "TypeError: props.addToCart is not a function." I am relatively new to Redux and have grasped the ...

A custom class that uses toggleClass() does not trigger an alert upon a click event

I am encountering an issue with the toggleClass() function in jQuery that I haven't seen addressed before. Despite successfully toggling the class of one button when clicked, I am unable to trigger a click event on the newly toggled class. Here is th ...

The Vue v-bind:class feature does not always update immediately when the value of the binded object is changed

When utilizing Vue, it seems that developers often use v-bind:class to add dynamic classes to elements. However, in the example below, this method appears to not function as expected. //Html <div id="mainapp"> <span class="star" v-bind:class="{ ...

What is the origin of the second parameter in an arrow function that returns another arrow function in a React component with Redux?

I'm having trouble understanding where the (val) parameter is coming from in the returned arrow function. I understand that max/minLength is an arrow function taking an argument set on the input field (3 and 25), and it returns another arrow function ...

Issues with retrieving data from Firestore and storing it into an array in a React

Struggling to fetch data from my firestore and display it on the webpage. Despite trying all possible solutions and searching extensively, I am unable to get it working. When using the code below, nothing appears on the website. However, if I switch to th ...

Can we actively send data updates from elasticsearch?

I'm currently developing an application that involves: Receiving streaming key-value data from a data provider (initial request receives all available data, subsequent requests provide updates) Storing this data in Kafka for persistence Pushin ...

Connecting Node.js with Express.js allows you to access the session data from the "upgrade" event

I'm currently facing a challenge with accessing the session object from an 'upgrade' event triggered by my Node.js server using the Express.js framework. Although I have successfully set up Session support and can retrieve it from the standa ...

send multiple textbox values to controller in CodeIgniter

I am new to Codeigniter and I'm facing some difficulties in understanding how to accomplish a task. In my view page, there are five rows generated using a for loop. Each row consists of two select boxes and two input boxes. I don't know how to re ...

The method for retrieving values and $id from a $firebaseArray using angularJS

Hey there, I'm fairly new to working with Firebase and I seem to be stuck on a problem that I can't find a solution for despite looking in many different places. Here is the structure of my Firebase database: I am trying to retrieve data from a s ...

Capturing information transmitted from an input device

I'm currently working on capturing input data from a piano connected to my computer via USB. $ lsusb ... Bus 003 Device 046: ID fc08:0101 .... The piano is identified as Bus 003 Device 046: ID fc08:0101. When I try $ cat /dev/bus/usb/003/046, th ...

Looking to enable only the user who created the post to have the ability to delete

In order to prevent a hacker from deleting data by knowing the rest api url and another user's userId, I am looking for a solution where it can be securely passed as a parameter. My plan is to store the jwt token in the database and verify it before ...

"I am trying to figure out how to set a link to an image using JavaScript. Can someone help me

I need help figuring out how to insert an image or gif file within two inverted commas '' in this line of code: _("status").innerHTML = ''; (line number 13 in the actual code) Your assistance with this question would be greatly appreci ...

Is it possible to include a submenu within a md-tooltip?

I have set up an md-tooltip for a sidebar, but I would like to enhance it by enabling a submenu option. For instance, if there is a Contacts submenu under the Profile menu, I want to be able to access Contacts when hovering over the Menu Icon. The tooltip ...

Using express-validator to validate an array parameter

Currently, I am implementing data validation for POST requests in my express application using express-validator. My form includes a select field where users can choose multiple options: <select name="category" multiple id="category"> <option ...

Ways to alter the default background color in material-ui

I am currently working on creating a dashboard for my company and I am looking to modify the default styles of some material components. Specifically, I want to change the background color of the Paper component without having to add my style to each ind ...