0

I have a very simple NodeJs script in which i am trying to append some text to a log file as soon as the page is loaded in the browser. The following is the script

var http = require('http');
var fs = require('fs');
var stream = fs.createWriteStream("mnodejs.txt");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
stream.once('open', function(fd){ stream.write("My first row\n"); }) ;
res.end('Hello World\n');
}).listen(8590, "127.0.0.1");

i suspect that , i have called the stream.once function at wrong place .

I also tried this way , but i am getting an error

var http = require('http');
var fs = require('fs');
var log = fs.createWriteStream('nodejs_log.txt', {'flags': 'a'});
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
log.write("this is a message");
res.end('Hello World\n');
}).listen(8590, "127.0.0.1");

error:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:632:11)
    at Array.0 (net.js:733:26)
    at EventEmitter._tickCallback (node.js:192:40)
Rahul
  • 10,343
  • 17
  • 58
  • 76
  • Possible duplicate: http://stackoverflow.com/questions/3459476/how-to-append-to-a-file-in-node – Alex Ciminian Feb 09 '12 at 14:07
  • 1
    The error has nothing to do with the logging code. You have another instance of you node server already listening on TCP port 8590 causing the EADDRINUSE error. Kill the other process and restart your server and you won't see that error. – Peter Lyons Feb 09 '12 at 15:39
  • yup , that worked .... :) thanks a lot.\ – Rahul Feb 09 '12 at 15:54

0 Answers0