Reputation: 121
I am doing an application with electron and I have the following problem:
I need to make an http request to receive data from a php but the timeout should be less than the response time and therefore cancel the request before delivering anything to me.
Anyone know how to lengthen the wait time on http request?
I leave you the code
var http = require('http');
var options = {
host: localStorage.getItem('server'),
port: localStorage.getItem('port'),
path: localStorage.getItem('directori') + '?nosession=1&call=ciberFiSessio&numSerie='+ localStorage.getItem("pc")
};
http.get(options, function(res) {
alert("hola");
if (res.statusCode == 200){
//reinicia();
res.on('data', function (chunk) {
str = chunk;
alert(str);
var myJSON = JSON.parse(str);
//alert(myJSON.fi);
if(parseInt(myJSON.fi)==0){
alert("Hi ha hagut un problema!");
}else{
reinicia();
}
});
}else{
alert("El lloc ha caigut!");
alert(res.statusCode);
}
}).on('error', function(e) {
alert("Hi ha un error: " + e.message);
});
Upvotes: 0
Views: 1179
Reputation: 4024
I assume you want extend the timeout time of the node http request, to wait for the PHP server to responde.
You can set the timeout
property of the http request in milliseconds.
Just add the property to your options object, for example:
var http = require('http');
var options = {
timeout: 1000, // timeout of 1 second
host: localStorage.getItem('server'),
port: localStorage.getItem('port'),
path: localStorage.getItem('directori') + '?nosession=1&call=ciberFiSessio&numSerie='+ localStorage.getItem("pc")
};
http.get(options, ...)
from the official documentation:
timeout : A number specifying the socket timeout in milliseconds. This will set the timeout before the socket is connected.
Read more about http.request (as it accepts the same options as http.get
).
Upvotes: 0