expressjs / serve-static

Serve static files
MIT License
1.4k stars 228 forks source link

why serve-static did not call next() method like other middleware #68

Closed kimown closed 8 years ago

kimown commented 8 years ago

I want to modify the response body after I use the serve-static middleware , but I am very confused why serve-static only call next() when error happens, I saw other middleware's code ,like compression,cookie-session ,finally they all call next() method.

dougwilson commented 8 years ago

Hi @kimown, the reason this module does not call next() unless it's an error is because in Express and the middleware pattern, you only call next() if you either (a) encountered an error or (b) did not write out a response body.

Since this module writes out a response body, it does not call next(), as this module already handled the request by sending a response. All the other examples you gave (compression and cookie-session) are not sending a response back, thus they are calling next() in the non-error case.