0
votes

I use fs library for reading file from system. I don't know I use this library and meet error.

  1. I'm using PhpStorm. at line : fs.readFile() : there's a line under that, that notice me : unresolved function or method readFile(). It means IDE doesn't figure out where is this function. Nevertheless, I have checked fs.js and I see no problem.

  2. I receive this error when running:

events.js:72 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE at errnoException (net.js:901:11) at Server._listen2 (net.js:1039:14) at listen (net.js:1061:10) at Server.listen (net.js:1127:5) at Object. (/home/hqt/PhpstormProjects/NodeApp/sample/AsynchronouslyReading.js:21:4) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10)

Here is my code :

var http = require('http');
var fs = require('fs');

// create http server
http.createServer(function(req, res) {
    fs.readFile('helloworld.js', 'utf-8', function(err, data) {
        res.writeHead(200, {'content-type' : 'text/plain'});
        if (err) res.write('could not find or open file');
        else res.write(data);

        // ending
        res.end();
    });
}).listen(8124, function() {console.log('server is running at 8124');});

console.log('server is runnint at 8124 port');

Please help me figure out why. I'm using Ubuntu machine for development.

Thanks :)

1

1 Answers

2
votes

That's because something else is already listening on port 8124. On Linux, you can use something like netstat -tanp | grep LISTEN | grep 8124 to see what.