Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
How to export function as function expression in express node js

I have a function

exports.loginService = async(req,res) => {
  const { workerId, workerPassword } = req.body;
  try {
    const foundUser = await User.findOne({ workerId });
    if (!foundUser) {
      return res.status(404).json({
        message: "Employee does not exist (Invalid Employee ID)",
      });
    } else if (foundUser.totalLoginAttemptLeft > 0) {
      const { updatedWorker, isMatch } = await checkPassword(
        workerPassword,
        foundUser
      );
      if (!isMatch) {
        passwordNotMatched(res, updatedWorker);
      } else {
        const jwtToken = await totalLogin(foundUser, reset);
        return res.status(200).json({ token: "Bearer " + jwtToken });
      }
    } else {
      return res.status(400).json({
        message: "You'r account is locked Please contact to ADMIN or HR",
      });
    }
  } catch (error) {
    console.error(error.message);
    return res.status(500).json({ message: error.message });
  }
}

I am exporting this in the main route file

const loginService = require("../../services/authServices")

router.post("/login", loginService);

But when I running the code it's giving this error

Error: Route.post() requires a callback function but got a [object] Object

What I am doing wrong?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

your file exports an object containing a function called loginService, so you should do something like:

const authServices = require("../../services/authServices")

router.post("/login", authServices.loginService);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You're trying to use the exports object as though it were a function. If you want to only export the function, overwrite exports rather than assigning to a property on it:

module.exports = async(req,res) => {
// ...
};

(Note that it has to be module.exports, not just exports.)

Alternatively, keep your current export and destructure the require call:

const { loginService } = require("../../services/authServices");
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda