Home > Software engineering >  Empty object in post request Express.JS
Empty object in post request Express.JS

Time:11-14

I'm working with Express v^4.18.2, I have some routing and controller files and an html form with post method. When I make the post, I try to get the server to show me the req.body sent in the request through the console, but I get an empty object. It should be noted that I am following a tutorial and I do it step by step. Could someone with more knowledge of Express tell me what is going on?

Routes.js

import { Router } from "express";
import dashBoardObject from "../Controller/DashboardsController.js";
import { DashboardContoroller} from "../Controller/DashboardsController.js";

const router = Router(); 

const { formDashboardController,
        postFormDashboardController,
        putDashboardContoller
    } = dashBoardObject;

router
   .get('/DashBoard', DashboardContoroller)

   .get('/DashBoard/createdDasboard', formDashboardController)


router
   .post('/DashBoard/createdDasboard', postFormDashboardController)


router
   .put('/DashBoard/updateDashboard', putDashboardContoller)


export default router;

As you can see, in the routes I try to destruct the object that I get from the controller.

Controller.js

const dashBoardObject = {};

export const DashboardContoroller= (req, res) =>{
    res.send(`indicators/Indicators`);
} 

dashBoardObject.formDashboardController = (req, res) =>{
    res.render(`indicators/Indicators`);   
}

dashBoardObject.postFormDashboardController = (req, res) =>{
    // let values = Object.values();
    console.log(`request: ${req.body}`);
    res.send(`<h3> CREATED!            
  • Related