Sign in
Log inSign up

ExpressJS, protected routes.

Imran Khan's photo
Imran Khan
·Jan 27, 2018

Hi everyone,

Is there better way to have protected routes, in expressjs, I'm sensing some code messy while code expands while i'v to repeat the middleware to all the routes.

Any better way to initialize like in Laravel group routes. My code below.

router.post("/create", controller.authController.isAuthenticated, controller.articleController.create)
router.get("/edit/:id", controller.authController.isAuthenticated, controller.articleController.edit) 
router.put("/update/:id", controller.authController.isAuthenticated, controller.articleController.update)
router.delete("/delete/:id", controller.authController.isAuthenticated, controller.articleController.delete)
Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2025 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct