test_112mo ago

whats the difference between context and middleware

and which 1 should I use for express session cookies
1 Reply
mark salsbery
mark salsbery12mo ago
Context is data, middleware is code. Middleware allows you to add reusable code to your procedures - code that runs before and/or after your procedure code. Your context data is typed, and available in all your procedures and their attached middleware. Your context is created on every request, so one way you could handle session cookies is to extract the cookie from the request headers in your createContext function and add the desired session data to your context. Session cookies could be set on the response headers of a login procedure, The docs show examples… https://trpc.io/docs/server/context https://trpc.io/docs/server/middlewares
Context | tRPC
Your context holds data that all of your tRPC procedures will have access to, and is a great place to put things like database connections or authentication information.
Middlewares | tRPC
You are able to add middleware(s) to a procedure with the t.procedure.use() method. The middleware(s) will wrap the invocation of the procedure and must pass through its return value.