Node.js update call resources twice?

I have a basic node.js setup, but I think something is missing.

My main.js :

 var a = require('./another.js'); var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, { 'Content-Type': 'text/html' }); response.write(a.saysomething('Hi there!') + '<br />'); response.end(); }).listen(27182); console.log('Server running at http://127.0.0.1:27182/'); 

My another.js :

 exports.saysomething = function (str) { console.log('in saysomething!'); return str + ' hey there!'; } 

The problem is that my console prints in saysomething! twice. Am I missing something? When I in saysomething! page, I expect only one in saysomething! .

+10
javascript
Nov 27 '11 at 5:16
source share
2 answers

Your browser will most likely try to get favicon.ico in addition to the url you click on. Try simply running the command on the command line: curl http://127.0.0.1:27182/

Alternatively, you can add a log for request.url . This will show you which URLs are being requested.

+30
Nov 27 '11 at 5:29
source share

Here is another way to understand the problem. This node server will track all requests and show them to you in a browser in JSON. Then you can press F5 and see the requests you make.

  var http = require("http"); var messages = []; http.createServer(function(request, response) { console.log("We got a hit @ " + new Date()); messages.push(request.url); messages.push(request.headers); response.writeHead(200, {"Content-Type": "text/plain"}); for ( var i = 0; i < messages.length; ++i ) { response.write("\n\n" + JSON.stringify(messages[i])); } response.end(); }).listen(8888); 

PS: See also this duplicate for more information nodejs - http.createServer seems to be calling twice

0
Mar 29 '13 at 18:45
source share



All Articles