Importing and Exporting Module Variables - Node.js

Hi all,

I have a route of type ‘post’, that is receiving some info from the front-end, as shown below:

const router = require("express").Router();
const GameModel = require("../models/game.model");
const UsernameController = require("../controllers/username.controller");"/username", async (req, res) => {

    try {
      const cookie = req.cookies;
      const {userName} = req.body;
      let allGames = await new UsernameController().getGames(userName);
      return res.sendStatus(200)
    } catch (err) {

  module.exports = router;

I need to use the destructured {userName} = req.body in another middleware.js file. So I’m wondering how I can export the {userName} received from the fornt-end to the middleware.js file.
The middleware.js file is as shown below:

const AuthController = require("../controllers/auth.controller");
const UsernameController = require("../controllers/username.controller");
const usernameRouter = require('../routes/username.route')

module.exports = async (req, res, next) => {
  let userName = usernameRouter.userName
  const userGamesArray = await new UsernameController().getGames(userName)
  req.userGamesArray = userGamesArray;

When I console.log the userName variable in the middleware file, it responds with undefined; which means I’m importing the variable wrongly from the route.js file.

Kindly assist me with importing the variable from the route.js file to the middleware.js file.

I don’t see you calling next in"/username") callback. How are you passing execution to the next middleware? Besides how are you mounting the router in your server? The express documentation will most likely be of help to you here. Check out how to mount a series of middleware functions at a given route in the link below.

Thanks @nibble , I’ll read through the link.