javascript - How request and response event are different from each other in node js? -
i tried simple server in nodejs. expected output of program
server console -
- request recieved 0
- request recieved 1
- request recieved 2
- request recieved 3
client expected response
- you have visited site many times - 0
- you have visited site many times - 1
- you have visited site many times - 2
- you have visited site many times - 3
client actual response
- you have visited site many times - 1
- you have visited site many times - 3
i couldn't understand behavior. , following code ran
//dependencies var http = require('http'); //variables var counter = 0; //callbacks , functions var requesthandler = function( request , response ) { console.log('request recieved ' + counter++); response.writehead(200); response.end('you have visited site many times - '+counter); }; //objects var server = http.createserver(requesthandler); //executions server.listen('8080');
what possible reason behavior ?
upon investigating, tried following code on server
console.log(request.url)
and outputs
/ /favicon.ico
turns out browser sends 2 requests server.
- localhost:8080
- /favicon.ico
this increasing request count 2 whenever page reloaded. here expected requests count 1 actual 2
Comments
Post a Comment