4

I have an API written using the express framework. As part of the logging/scaling-tests process, I'd like to know how large a received request is in bytes (so we can determine the effect it's going to have on the network).

Is there a way of doing this? I've read through the Express Documentation and couldn't find anything. Will reading off the content-length header work? Or could that value be sent incorrectly?

user2110845
  • 552
  • 7
  • 17

2 Answers2

6

I have most commonly seen code using the content-length header, for example with req.get("content-length"). However I recently ran into a situation where that header wasn't provided, so I switched to using req.socket.bytesRead.

Carl
  • 354
  • 4
  • 8
  • 1
    what req.socket.bytesRead will give? I don't think it is giving req body size. I checked both for the same request "content-length" gave me 138421 and socket bytes - 65536 so I don't think they are same – Abhay Sehgal Feb 20 '20 at 05:58
-3

I hope you are using express.bodyparser. Please check the body parser docs. The default value of body parser 100kb. It will only accept 100kb size data for manipulation. Otherwise it will throw Error.

You can increase the limit by passing limit options. Check the following link to increase request size Error: request entity too large

Community
  • 1
  • 1
Kundu
  • 3,300
  • 3
  • 13
  • 20
  • 3
    I don't need to worry about the size limits. I'm trying to figure out what size it is for logging and scaling purposes. – user2110845 Sep 23 '16 at 06:38
  • @user2110845 Are you trying to evaluate each load in request or you are trying to calculate overall load per second? – Kundu Sep 23 '16 at 06:42
  • The load in each request. – user2110845 Sep 23 '16 at 06:44
  • You can use this limit option to clarify the size option. It will determine how much request size body parser will process for each request. – Kundu Sep 23 '16 at 06:45
  • 3
    I don't want to limit the size of the requests, I want to know how large each request is so we can calculate the expected load – user2110845 Sep 23 '16 at 06:48
  • Hi user2110845, Maximum express server can take 100kb default load unless you have manipulate the limit. – Kundu Sep 23 '16 at 07:00
  • Please check with higher load - may be send some base64 image of 1mb size. – Kundu Sep 23 '16 at 07:01