GET request equivalent in separate files (controller and route)

Asked

Viewed 55 times

1

Good night!

Working on a project, and on the login screen after performing the authentication (Passportjs), the user will be redirected to the home page.

However, a little while ago I started to adopt some new methods to better organize the structure of the program etc. And I am leaving a file only for routes (Routes) and another only to manage the calls GET, POST, DELETE, PATCH, PUT (controllers).

Before migrating the files to this new format, everything worked normally... In this piece of code I’m in trouble... In this format everything works normally

app.get("/home", isAuth, (req, res) => {
    res.render("home", {
      name: req.user.username
    });
  });

isAuth is a module to check whether the user is authenticated or not, and if so, will be redirected.

module.exports = {
  isAuth: (req, res, next) => {
    if(req.isAuthenticated()) {
      return next();
    } else {
      req.flash("error_msg", "Please, log in to view this page");
      res.redirect("/login");
    }
  }
}

In this format I get the error "Cannot GET /home"

// @desc Home Page
// @route GET /home
// @acess Private
exports.getHome = (isAuth),(req, res) => {
  res.render("home", {
    name: req.user.username
  });
};

Can anyone tell me what I’m doing wrong?

  • How are you importing this new module? And how are you connecting it to Express?

  • It seems to me that the error is at the time you export. Try to export an array: exports.getHome = [isAuth, (req, res) => {
 res.render("home", {
 name: req.user.username
 });
}].

  • I am importing in this way: const { isAuth } = require(".. /config/auth");

2 answers

1


Your problem is in the syntax of exports.getHome = (isAuth),(req, res) => {. That one isAuth (which is a middleware) cannot be exported that way.

You could export an array with these two functions and then use app.get("/home", ...controlers).

I usually do like this:

Routes:

const {isAuth} = require('./isAuth.js');
const controllers = require('./controllers.js');
app.get("/home", isAuth, controllers.getHome);
app.get("/store", isAuth, controllers.getStore);

isAuth.js:

module.isAuth = (req, res, next) => {
  if(req.isAuthenticated()) {
    return next();
  } else {
    req.flash("error_msg", "Please, log in to view this page");
    res.redirect("/login");
  }
}

js controllers.:

exports.getHome = (req, res) => {
  res.render("home", {
    name: req.user.username
  });
};
exports.getStore = (req, res) => {
  res.render("store", {
    name: req.user.username,
    items: db.userItems // só um exemplo
  });
};
  • In the route file it looks like this: // Home Page router . route("/home") . get(getHome);

  • 1

    It worked out! Thank you!

0

Aaaaah!

With the information you passed I made some changes and it worked!

isAuth.js (middleware)

module.exports = {
  isAuth: (req, res, next) => {
    if(req.isAuthenticated()) {
      return next();
    } else {
      req.flash("error_msg", "Please, log in to view this page, yo!");
      res.redirect("/login");
    }
  }
}

Routes

const express = require("express");
const { getHome } = require("../controllers/users");
const { isAuth } = require("../config/auth");

// Home Page
router
  .route("/home")
  .get(isAuth, getHome);

users.js (Controller)

const express = require("express");
const router = express.Router();

// @desc Home Page
// @route GET /home
// @acess Private
exports.getHome = (req, res, next) => {
  res.render("home", {
    name: req.user.username
  });
};

:D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.