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

101
Views
ExpressJS Route Async Middleware

I'm trying to use a middleware in Express but I can't get it to work I get infinite loading time when I make a request. I searched on stackoverflow but I couldn't find any examples that used an async await middleware stored in a separate file. Can someone point me in the right direction?

isAuthenticated.js

const Auth = require('../auth/auth')

const isAuthenticated = async () => {
    return async (request, response, next) => {
        const token = request.cookies.token;
        try {
            const JWTVerify = await Auth.verifyJWTToken(token);
            next()
        } catch (error) {
            response.json({ status: "failed", message: "Authentication failed invalid token" });
            response.end();
        }
    }
}

module.exports = isAuthenticated

Server.js

const isAuthenticated = require('./middleware/isAuthenticated')

app.post('/some-route', isAuthenticated, async (request, response) => {


});

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You returning a function, and thus the middleware is not passing the request forward.

Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle. The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.

check ---> http://expressjs.com/en/guide/writing-middleware.html

const Auth = require('../auth/auth')

const isAuthenticated = async  (request, response, next) => {
        const token = request.cookies.token;
        try {
            const JWTVerify = await Auth.verifyJWTToken(token);
            next()
        } catch (error) {
            response.json({ status: "failed", message: "Authentication failed invalid token" });
            response.end();
        }
    }


module.exports = isAuthenticated
about 4 years ago · Juan Pablo Isaza Report

0

I think the issue is the definition of your middleware. You are wrapping it inside a function, but you are not calling it. If you pass it like that to express, express will try to call it but the outer function just returns the inner function. The inner function thus never gets called so next() never gets called.

const isAuthenticated = async (request, response, next) => {
    const token = request.cookies.token;
    try {
        const JWTVerify = await Auth.verifyJWTToken(token);
        next()
    } catch (error) {
        response.json({ status: "failed", message: "Authentication failed invalid token" });
        response.end();
    }
}
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!