rknell / alfred

A performant, expressjs like server framework with a few gadgets that make life even easier.
Other
526 stars 29 forks source link

req.headers.add causes errors #117

Open chariot6 opened 1 year ago

chariot6 commented 1 year ago

I was following the example for Middlewares in the README and noticed that req.headers.add gives me a HttpException: HTTP headers are not mutable error. Accessing the headers from res instead seems to work as expected.

d-markey commented 1 year ago

This is by design, req.headers are those sent by the browser so they're part of the incoming request. You cannot tamper with them. res.headers on the other hand, are sent with the response to the request so you can define whatever headers you need. If you want your middleware to add information to the incoming request, you need to find another way to augment the request with some contextual info. If you're looking for some sort of key/value storage, maybe you could use the store_plugin that Alfred provides out of the box? See req.store.

rknell commented 1 year ago

It's a valid bug though - someone made a stupid typo in the example document (very likely that someone was me!). I was half way through fixing the documentation when I got sidetracked today. Just need to change the example from req.headers to res.headers

chariot6 commented 1 year ago

Sorry if I wasn't clear. My complaint was with the documentation, not so much the design. :)

Outside of the README, req.headers.add also appears here. https://github.com/rknell/alfred/blob/496b865610e8743fc8f2b0308a5e275e5b97a20f/example/example_middleware_2.dart#L7

Feel free to close this issue. Thanks for the quick replies.

rknell commented 1 year ago

Thanks - early on to make sure the docs were at the very least valid dart and not pseudocode I made a little generator that pulls in the actual dart files and injects them in the readme.