Home > Software engineering >  How to pass request body object to other routes?
How to pass request body object to other routes?

Time:09-21

Anyone know the solution? When I console log the variable it shows undefined. I need to pass the req.body (name and email) element to second post route which is "/company" route. I have two forms and I want to pass the first form data to second form the "/company" route form. then the two form data will be stored in mongoDb. I tried everything but I got only undefined in second route the "/company" route

import express from "express";
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
 
//Assign the name and email to variable and sent to last route
const middleware = (req, res, next) => {
  const { name, email } = req.body;
  let nameNew = name;
  let emailNew = email;
  // add nameNew and emailNew to req object
  // so that it can be access in other routes
  req.nameNew = nameNew;
  req.emailNew = emailNew;
  // console.log(nameNew);
  // console.log(emailNew);
  next();
};
 
//Get name email from user input and sent it middleware
app.post("/", middleware, (req, res, next) => {
  const { name, email } = req.body;
  res.send("Home route");
});
 
//Get the name and email from middleware
app.post("/company", middleware, (req, res) => {
  // console.log(nameNew);
 
  // access req.body without middleware
  // console.log(req.body);
 
  // access req.body using middleware
  console.log(req.nameNew);
  console.log(req.emailNew);
 
  res.send("Company route");
});
 
//Server running
app.listen(4000, () => {
  console.log(`Server Started `);
});

CodePudding user response:

When your request gets into your app.post("/") it sets the nameNew variable inside your middleware scope. You cannot access this variable in your other routes.

If you want to access variables shared from your middleware you can use res.locals : https://expressjs.com/en/api.html#res.locals

You can't pass data from route 1 to route 2 through middleware.

It passes through the middleware and sets the variables but on your res.send theses variable will disappear. These are just set for the "instance" of the incoming request and on res.send will be destroyed.

You would need to send back data to the front-end to pass it back to your API for route 2.

You can call functions from your route definition :

    app.get('/getUserInfos', isLoggedIn, (req, res) => {
            return getUserInfos(res);
        });

This way you can process your data and save it however you want.

Also, looks like you can chain functions there : https://stackoverflow.com/a/62723674/12343548

CodePudding user response:

I use req.app.locals to send the data in first route to second route. Anyone want to do this type of program in future Here is the solution.

const express = require("express");
const app = express();
const path = require("path");
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const middleware = (req, res, next) => {
  const { name, email } = req.body;
  req.app.locals.nameNew = name;
  req.app.locals.emailNew = email;
  next();
};

app.get("/", (req, res) => {
  res.sendFile(path.join(__dirname   "/index.html"));
});

app.post("/", middleware, (req, res) => {
  const { name, email } = req.body;
  res.send("Home Route");
});

app.get("/company", (req, res) => {
  res.sendFile(path.join(__dirname   "/company.html"));
  console.log(req.app.locals.nameNew);
  console.log(req.app.locals.emailNew);
});

app.post("/company", (req, res) => {
  console.log(req.app.locals.nameNew);
  console.log(req.app.locals.emailNew);
  res.send("Company Route");
});

//Server running
app.listen(4001, () => {
  console.log(`Server Started `);
});
  • Related