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

118
Views
¿Puedo usar la ruta dentro de un controlador en express js?

Así que tengo algo como esto en uno de mis controladores:

 module.exports.authToken = (req, res, next) => { const token = req.cookies.jwt; //console.log(token); if (!token) { return res.sendStatus(403); } try { const data = jwt.verify(token, "secret token"); console.log(data); req.userId = data.id; return next(); } catch { return res.sendStatus(403); } };

y es llamado por una ruta:

 router.get("/protected", authController.authToken, (req, res) => { return res.json({ user: { id: req.userId, role: req.userRole } }); });

y quiero obtener una respuesta JSON de esa ruta en uno de mis otros controladores. Intenté algunas cosas pero ninguna funcionó.

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

0

Lo que haría sería abstraer la respuesta a una función para su reutilización:

 // the function will just return the data without writing it to the response function protectedRoute(req) { return {user: {id: req.userId, role: req.userRole}}; } router.get("/protected", authController.authToken, (req, res) => { // in the actual handler you can return the response return res.json(protectedRoute(req)); }); // make sure the middleware is still being run router.get("/other_route", authController.authToken, (req, res) => { // use the same function to get the response from /protected const protectedResponse = protectedRoute(req); // do stuff with it });
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!