Wednesday, 10 May 2017

Why the code is slow,how to speed it up

The code and what i want to do:

var express = require("express");
var app = express();

var responses = [];

app.get('/', function (req, res) {
  responses.push(res);

  if (responses.length == 3 /*or after some times (0.1s maybe)*/){
      // merge the requests and query database once not 3 times

      // then dispatch query result for requests

      // finally send result back
      responses.forEach(function (r){
          r.send("hello");
      })
  }
});

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});

I request the api with browser for 3 times,and after a long-time waiting,i get the response "hello" from server.

What is wrong with the code,and how to do it properly.



via code_farmer

No comments:

Post a Comment