fs library : Error when use it - node.js

I use fs library for reading file from system. I don't know I use this library and meet error.
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.
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 :)

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.

Related

address already in use :::8080 but port isn't being used by any process

I ran this code:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
I am getting the following error:
Error: listen EADDRINUSE: address already in use :::8080
at Server.setupListenHandle [as _listen2] (net.js:1290:14)
at listenInCluster (net.js:1338:12)
at Server.listen (net.js:1425:7)
at Object.<anonymous> (/home/dell/foo.js:6:4)
at Module._compile (internal/modules/cjs/loader.js:707:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
at Module.load (internal/modules/cjs/loader.js:605:32)
at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
at Function.Module._load (internal/modules/cjs/loader.js:536:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:760:12)
Emitted 'error' event at:
at emitErrorNT (net.js:1317:8)
at internalTickCallback (internal/process/next_tick.js:72:19)
at process._tickCallback (internal/process/next_tick.js:47:5)
at Function.Module.runMain (internal/modules/cjs/loader.js:763:11)
at startup (internal/bootstrap/node.js:303:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:872:3)
So, here is some code I invoked in order to find out the process running at the port in the terminal:
❯ fuser 8080/tcp
~
But it turns out there is no process running on that port.
How do I fix this?
(I am new to node.js. I will appreciate any help in the right direction.)

Hapi js - makemehapi Issue

Hi New Node and I am practising tutorial "makemehapi" In the third assignment I have receiving below error. Any one can point where I am doing wrong?
Regards,
Surya
------------ Code ---------------------
var Path = require('path');
var Hapi = require('hapi');
var Inert = require('inert');
var server = new Hapi.Server();
server.connection({
host: 'localhost',
port: Number(process.argv[2]|| 8080)
});
server.register(require(Inert),function(err){
if(err) throw err;
} )
server.route({
method:'GET',
path:"/index.html",
handler: {
file: "index.html"
}
})
server.start(function () {
console.log('Server running at:', server.info.uri);
});
---------------------- error--------------------------
assert.js:86
throw new assert.AssertionError({
^
AssertionError: path must be a string
at Module.require (module.js:364:3)
at require (module.js:384:17)`enter code here`
at Object.<anonymous> (/home/surya/Desktop/nodeschool/Hapi Practice/test/Lesson3.js:13:17)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
✗ Error connecting to http://localhost:18384: ECONNREFUSED
events.js:85
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
You are requiring in Inert twice. Remove the require around the Inert variable in the following line:
server.register(require(Inert),function(err){
if(err) throw err;
});
To:
server.register(Inert,function(err){
if(err) throw err;
});

Socket.IO Server Runtime Error/Exception

I have this code in a file named server.js:
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){
console.log("a client connected");
});
server.listen(80);
When i run node server.js in the terminal (cmd), it throws this error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1023:19)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Object.<anonymous> (C:\inetpub\wwwroot\socketio\server.js:9:8)
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)
Do you have an idea of what could be the problem? Thanks!
Note: I already have socket.io and express installed.
It seems you are not allowed to open a serversocket on port 80.
See this line in the error message
C:\inetpub\wwwroot\socketio\server.js:9:8
Socket addresses smaller than 1024 are reserved for system use. Try this:
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){
console.log("a client connected");
});
server.listen(3000);
Looks like the port 80, is invalid or unacceptable. I tried to change it to another port (8000) and it worked.
server.listen(8000);
The exception is gone. I hope I can find a way to distinguish between valid/invalid ports at run-time.

EACCES error when running node app on iisnode

I'm struggling to get a simple node application to run on IIS using iisnode. Here is the application:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello, world! [helloworld sample; iisnode version is ' +
process.env.IISNODE_VERSION + ', node version is ' +
process.version + ']');
}).listen(process.env.PORT);
Here is the error that I'm getting:
Application has thrown an uncaught exception and is terminated:
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1127:5)
at Object.<anonymous> (C:\Program Files\iisnode\www\helloworld\hello.js:6: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 Module.require (module.js:364:17)
Because this is on my dev box I've given the app_pool superuser privileges but that hasn't helped.

what is blocking my node.js code? taking an argument from the command line

I am trying to take the first command line argument as the port to run node on.
The following code is not working in node.js:
try {
if(process.argv[2] == undefined) {
throw new Error("no port specified");
}
var port = process.argv[0];
console.log(port);
}
catch (err) {
console.log("Error give port number as the argument");
return;
}
require('http').createServer(function handleRequest(req, res) {
res.writeHead(200, {'content-type' : 'text/plain'});
res.end('Hello World!');
}).listen(port);
It gives the following error:
$ node server.js 8080
node
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:884:11)
at Server._listen2 (net.js:1003:19)
at listen (net.js:1044:10)
at Server.listen (net.js:1104:5)
at Object.<anonymous> (/home/studiet/Documents/Aptana Studio 3 Workspace/nodef2b/server.js:19: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)
I know that node.js is asyncronous, but in this case I thought nothing was waiting for anything. What is wrong? How can I make the system wait for whatever is taking too long here, and what is taking too long? Or, what else is wrong?
EDIT: line 19 in the code is where I .listen(port);
EADDRINUSE stands for Error Address In Use.
Some other process is using port 80.
You need to find that process and terminate it with extreme prejudice.
EDIT: In this case, it means an invalid port number.
As you can see from your first line of output, port is "node", not 8080.
You need to set it to argv[2].

Resources