Here is the code:
FILE: server.js
var express = require('express'),
app = express(),
useragent = require('express-useragent'),
compression = require('compression'),
vhost = require('vhost');
app.use(vhost("blabla.com", 'app.js'));
app.disable('x-powered-by');
app.use(compression());
app.use(useragent.express());
app.listen(80);
FILE app.js
var useragent = require('express-useragent'),
express = require('express'),
sessao = require('express-session'),
armazenamento_ficheiros = require('session-file-store')(sessao),
compression = require('compression'),
app = express(),
favicon = require('serve-favicon');
global.uuid = require('uuid');
app.all('/u/my.json', function (req, res) {
var body = '';
p.on('data', function (d) {
body += d;
});
p.on('end', function () {
var g = require('./gateway');
g.e(body,res);
}, 'utf-8');
});
module.exports = app;
FILE:gateway.js
module.exports = {
e: function (body,res) {
//connects to database using data in body and send results to client....
res.header("Cache-Control", "no-cache, no-store, must-revalidate");
res.header("Pragma", "no-cache");
res.header("Expires", 0);
res.header('Last-Modified', (new Date()).toUTCString());
res.header("Content-type", "application/json; charset=utf-8");
res.send(JSON.stringify({"a":1}));
}
};
The problem is that sometimes, the client makes two http requests and randomly, both are replied in a unique answer instead of each http request getting a corresponding reply. The problem seems to be when one of the http request makes a request to a database (creating a delay in getting the answer) and gets replied in another request. Since i send the headers, it gives error.
I check that the gateway.js file is loaded and executed with each http request but i do not understand why they get mixed. What is wrong with my code? Any help will be appreciate. Thank you.