express icon indicating copy to clipboard operation
express copied to clipboard

Some fetch similarities

Open jimmywarting opened this issue 3 years ago • 4 comments

I was wondering if we could have a few of the methods that exist on the window.Request.prototype since 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

  • Request.prototype.arrayBuffer
  • Request.prototype.text
  • Request.prototype.formData
  • Request.prototype.blob
  • Request.prototype.json

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

// manual body parser
app.post(path, function (req, res, next) {
  const chunks = []
  req.on('data', data => chunks.push(data))
  req.on('end', () => {
    const body = Buffer.concat(chunks)
    // do something with the hole body
    req.body = JSON.parse(body.toString())
    next()
  })
}, routeHandler)
// or
app.use(bodyParser.json(), routeHandler)

we could just do what is normally done in service worker & the fetch api:

app.post(path, async function routeHandler (req, res) {
  const arrayBuffer = await req.arrayBuffer()
  const text = await req.text()
  const blob = await req.blob()
  const json = await req.json()
  const formData = await req.formData()
})

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.

jimmywarting avatar Feb 23 '21 13:02 jimmywarting

@jimmywarting There is a module body-parser that handle this cases. We don't need to implement the same thing in express.

getspooky avatar Apr 08 '21 10:04 getspooky

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.

jimmywarting avatar Aug 20 '21 18:08 jimmywarting

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:

  • https://github.com/node-body
  • https://www.npmjs.com/package/@body/json

The idea was that you should be able to just await the body wherever you need it, yielding the same benefits that you outlined in the first post 🙌

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)
  }
})

// ...

LinusU avatar Aug 21 '21 09:08 LinusU