New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some fetch similarities #4536
Comments
|
@jimmywarting There is a module |
|
The body-parser don't really to the same thing. the body-parser is a middleware that blocks the actual route handler that it should call after you have received the hole body. With this solution you call the handler first so you can validate cookies and other request headers and so on before you get the hole body. |
|
I really like this idea, I haven't thad time to think about it enough to know if I think it should be included directly in Express, or be an official middleware though. But my spontaneous thought is that including it would be nice! I actually worked on something quite similar here: The idea was that you should be able to just e.g. const getJsonBody = require('@body/json')
// ...
app.post('/v1/users', async (req, res, next) => {
try {
const body = await getJsonBody(req)
// ...
} catch (err) {
return next(err)
}
})
// ... |
I was wondering if we could have a few of the methods that exist on the
window.Request.prototypesince we are all mostly familiar with working with fetch on frontend and backend (using node-fetch/undici). And maybe have it work a little bit like a service worker.namely
All of this returns a promise with the hole body being parsed. so you don't have to work with streams
So instead of having to do this blocking request (that don't call the next handler until it's finish
we could just do what is normally done in service worker & the fetch api:
The benefit of this would be that your route handler would be able to retrieve request headers first. So you can validate more quickly if a user is authenticated, if the payload is too large by looking at the content-length, what kind of content-type they are sending etc
ofc it is also possible to do that already but it would would require 3
handlers and look something like:
app.post(authenticate, bodyParser.json(), handler)I don't know so much about formData since it requires some more work/parsing and it would require some formdata package
Node 15.8 introduced buffer.Blob so we also have that.
The text was updated successfully, but these errors were encountered: