Thursday 19 July 2018

why NodeJS express' POST handler not called but RAM rise up?

I'm using NodeJs 8.11.3

I have two applications: the first one provides a simple Express' hello world API, plus some other libraries:

// API.js
const express = require('express')
const app = express()

var compression = require('compression')
var bodyParser = require('body-parser');

var helmet = require('helmet');
app.use(helmet());
app.use(compression());
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));



app.post('/', (req, res) => {
        res.send('Hello World!');
        console.log(`SENT in the FIRST PLACE at: ${req.body.event_datetime}, now is ` + new Date().toLocaleString());
});


app.listen(8000, () => console.log('Example app listening on port 8000!'))

and the second one is the API's client. The client sends a post request where the body is a JSON object of 3.5MB.

// client.js
const rp = require('request-promise');
function send_stuff(obj) {      
  let options = {
    uri: "http://xxxxx:8000/",
    method: "POST",
    headers: "headers": {
            "Content-Type": "application/json"
        },
    body: JSON.stringify(obj)
  }
  return rp(options);
}

var fs = require('fs');
const content= fs.readFileSync("./big_file.txt").toString();
(async () => {    
  for (let i = 0; i < 1000; ++i) {
    send_stuff(
      {
        id: 1,
        field: 1,
        field2: 1,
        big_content: content,
        id_domain: 1,
        event_datetime: new Date().toLocaleString(),
        field3: -1,
        field4: 1
      }).then(r => {           
        console.log('ok');           
      }).catch(err => {           
        console.error("API ERROR");
      });

      //await sleep(500);

  }
})();

function sleep(ms){
   return new Promise(resolve => setTimeout(resolve, ms) );
 }

There are two things that I cannot understand completely:

  1. I cannot see any packet received by API application until the for-loop from the client ends
  2. Monitoring API's RAM consumption I noted that rise up until the application starts to process the requests.

Questions:

about 1), is it possible that the client cannot send packets because the for-loop is keeping the event-loop frozen?

about 2) is it possible that API's RAM continues to rise up because packets from the client are actually coming but very slowly, cause of the 3.5MB request body they are trying to send, so the POST handler in the API cannot be called until the packet is complete?



from why NodeJS express' POST handler not called but RAM rise up?

No comments:

Post a Comment