Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!