Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exporting a module in Node.js is causing an error

I am trying to get my head around the different ways you can export modules in Node.js and am running into an issue.

When exporting a module in node.js, the following works fine:

const jwt = require('jsonwebtoken')

//Verify token
exports.cookieJwtAuth = (req, res, next) => {
    const token = req.cookies.token
    try {
        const user = jwt.verify(token, process.env.AUTH_SECRET)
        req.user = user
        next()
    } catch (err) {
        res.clearCookie('token')
        return res.redirect('/login')
    }
}

However, when I try to export it like below I get this error:
Error: Route.post() requires a callback function but got a [object Undefined]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const jwt = require('jsonwebtoken')

//Verify token
const cookieJwtAuth = (req, res, next) => {
    const token = req.cookies.token
    try {
        const user = jwt.verify(token, process.env.AUTH_SECRET)
        req.user = user
        next()
    } catch (err) {
        res.clearCookie('token')
        return res.redirect('/login')
    }
}

module.exports = cookieJwtAuth

Why is the second code sample throwing the error, what am I doing wrong here?

>Solution :

Try

module.exports = {cookieJwtAuth}

to match it with the first code

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading