Express.js/Node.js Responses Before Query Finishes

I have a local server that is fully functioning when POSTs and GETs are sent from Postman or chrome rest apps etc. However, whenever I send it from an ESP8266 it fails as it follows:

This is my server side code:

router.get('/', function (req, res) {
var person = req.query.id;
console.log("1");
Person.find({person: personID} ,function (err, station) {
    if(err) console.log(err.message);

    console.log("3");
    res.send('test');
    console.log("4");
});

console.log("2");
});

Lines below are from console when request is sent from Chrome.

1
2
3
4
GET /api?id=ABKWTltz6Ug 200 21.829 ms - -

And the lines below are from console when request is sent from ESP8266.

1
2
GET /api?id=ABKWTltz6Ug - - ms - -
3
4

As you may notice logs 3 and 4 are after GET response line. My ESP8266 TCP connection lines match the HTTP Message Format:

GET /api HTTP/1.1
Host: *SERVER_IP_ADDRESS*

The problem raised when we switched from PHP to NodeJS backend server. ESP8266 used to connect and operate without problems back then. And FYI, everything also works fine if I remove the Person.find code block and move the res.send('test'); outside.

Another freshly written backend code to hopefully spot the error is below. This is the whole code. Which also returns the same error I've mentioned earlier above:

var express = require('express');
var app = express();
var router = express.Router();
var mongoose = require('mongoose');
var morgan = require('morgan');


app.use(morgan('dev'));

mongoose.connect('mongodb://localhost/test');


//mongoose.Promise = Promise;
var Test = mongoose.model('Test', { name: String });

router.get('/test', function (req, res) {

    console.log("1");
    var test1 = new Test({
      name: "Name"
    });

    var query = test1.save();

    query.then(function (doc) {
        res.send('1').end();
        console.log('3');
    });

    console.log("2");
});

app.use('/api', router);

var port = 9000;
app.listen(port,function(){
    console.log('server started on', port);

});

any help please ?

Can you post also the ESP8266 code which contains the Get request.

sure

cmd = F("GET /api/setTemperature?id=");
    cmd += ID;
    cmd += F("&temp=");
    cmd += getTemperatureFromSensors();
    cmd += F(" HTTP/1.0\r\n\r\nHost: ");
    cmd += SERVER_IP;
    String cipSendCmd = F("AT+CIPSEND=");
    cipSendCmd += cmd.length();
    sendCommand(cipSendCmd);
    sendCommand(cmd);

6v6gt:
Can you post also the ESP8266 code which contains the Get request.

do you have any idea ?

That is not the whole code but I guess that getTemperatureFromSensors() returns just digits.

My experience is with the ESP8266 Web classes and not AT commands. I did, however, find this:

Querry Web Page

The first thing we need to do is establish a connection to the webserver.  To do this you use the following command while obviously replacing the IP address with that of your web server.  In our case we are using standard HTTP (NOT SSL/HTTPS) therefore our port is 80.  I am not sure how this would work over SSL so if you figure it out please post in the comments below.

AT+CIPSTART=”TCP”,”192.168.88.35″, 80

Next we need to let the WiFi module know how many characters we are sending in our query.  To do this we issue a command like the following

AT+CIPSEND=50

Finally we can perform an HTTP get against the server. Note the number 50 above is the total number of characters in this command.  We count \r & \n as single characters and we do not count the last \r\n sequence.

GET /status.html HTTP/1.0\r\nHost: 192.168.88.35\r\n\r\n

This should result in a response from the webserver.  The last step left is to be proper citizens and close the connection by issuing the following command.

AT+CIPCLOSE

This is very cool stuff!  Now lets move this into something usefull and start writing some Arduino code.

source

This implies some blank lines after the Get command, and a CIPSTART and a CIPCLOSE command which may or may not appear in your code.

Edit:
maybe instead of . . .

    cmd += F(" HTTP/1.0\r\n\r\nHost: ");
    cmd += SERVER_IP;

this . . .

    cmd += F(" HTTP/1.0\r\nHost: ");
    cmd += SERVER_IP;
    cmd += F("\r\n\r\n");