-
Hi there, I am new on fastify Is there any way I can attach prehandler to specific prefix like express router? example express router // users.route.ts
const router = Router();
router.get("/", userController.getUsers);
router.put("/:id", userController.editUser);
// main.ts
app.use("/api/users", requireUser, usersRoutes); here's what I did so far, I thought that prehandler is executing only on this prefix app.register(
(instance, _, done) => {
instance.get("/", getUsers);
instance.put("/:id", () => {});
instance.delete("/:id", () => {});
done();
},
{ prefix: "/users" }
).addHook("preHandler", isAuthed); But the pre handler is executing on all of the routes |
Beta Was this translation helpful? Give feedback.
Answered by
Eomm
Sep 10, 2023
Replies: 1 comment 1 reply
-
Write instance.addHook instead. There is another technique using the onroute hook. I call it aspect oriented programming in my fastify book 👍 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
rndy28
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Write instance.addHook instead.
Your code applies the hook to the app instance
There is another technique using the onroute hook. I call it aspect oriented programming in my fastify book 👍