Unable to run even basic Node.Js file on raspberry pi - node.js

So I have installed node.js onto my raspberry pi. Checked that it was installed by running:
node -v
which yielded: 4.2.1 and
npm -v
which yielded 2.14.7. however when I try to run any .js file (that I have tested running in node on my pc) it fails with the following:
pi#raspberrypi:~/share/Node $ node Brew-View.js
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at Server.listen (net.js:1366:5)
at EventEmitter.listen (/home/pi/share/Node/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/pi/share/Node/Brew-View.js:39:8)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
I even went back to basics and just ran a file that just had
console.log('Hello.');
in it and got the same result. This sounds like it is an issue being caused by a bad preinstalled module. but it is a fresh installation(both of node and raspbian). is there some setup step that you have to do specifically for node on pi or call it differently?
Note: this is on a pi 3 B+
Edit: this is the code I am running which is yielding the 141 error(I have added ip and express to that directory via npm):
const express= require('express')
const ip = require('ip')
const ipAddr=ip.address();
const port=80;
const Server= express();
Server.use(express.static('public'))
Server.get('/',function(req,res){
res.send('<html>'+
'<head>'+
'<meta charset ="UTF-8">'+
'<link rel="stylesheet" type="text/css" href="./main.css"></head><body>'+
'<h1>Brew View</h1>'+
'<p>my IP is: '+ipAddr+":"+port+'</p>'+
'</body>'+
'</html>')
})
// Handle 404
Server.use(function(req, res) {
res.status(404).send('<html>'+
'<head>'+
'<meta charset ="UTF-8">'+
'<link rel="stylesheet" type="text/css" href="./main.css"></head><body>'+
'<h1>404: Page not Found</h1>'+
'</body>'+
'</html>');
});
// Handle 500
Server.use(function(error, req, res, next) {
res.send('500: Internal Server Error', 500);
});
Server.use(express.static('public'))
Server.listen(80,()=> console.log("Hello. Brew View is Hosted at: "+ipAddr+":"+port))
this was also occurring with a more basic http module based server as well.
Edit: so The solution was that you can't automatically host on port 80 without root.

Related

why node js socket not working when change port to https?

Normally in app.js i use this code on port 3000 it's work good (on my-domain.com:3000).
.
http.listen(3000, function(){
console.log('start server on port :3000');
});
then i want to use on https, so i change app.js to
http.listen(443, function(){
console.log('start server on port :443');
});
When run node app.js , it's show error
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::443
at Object.exports._errnoException (util.js:1012:11)
at exports._exceptionWithHostPort (util.js:1035:20)
at Server._listen2 (net.js:1252:14)
at listen (net.js:1288:10)
at Server.listen (net.js:1384:5)
at Object.<anonymous> (/home/admin/web/my-domain.com/public_html/app.js:28:6)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
Normally i have to access to my-domain.com:3000 for user chat. So i want to know how can i access to https://www.my-domain.com for user chat ?
You have the following error message:
Error: listen EADDRINUSE :::443
[...]
This message means that the port 443 is currently in use by some process.
You can check which process is actually using the said port by executing one of the many tools for network checking (such as netstat for Windows, lsof or netstat on Linux).
Refer to the manual for those tools to achieve the correct result, based upon your operating system.

Why i can not use express node js on my server?

node js version node -v ===> v6.3.1 was install on my server.
then i will install express by this step
npm init
in entry point: (index.js)
npm install express --save
then create app.js
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(3000, () => console.log('Example app listening on port 3000!'))
and then node app.js
and it's show error
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:1012:11)
at exports._exceptionWithHostPort (util.js:1035:20)
at Server._listen2 (net.js:1252:14)
at listen (net.js:1288:10)
at Server.listen (net.js:1384:5)
at EventEmitter.listen (/home/admin/web/my-domain.com/public_html/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/admin/web/my-domain.com/public_html/app.js:6:5)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
how can i do ?
Choose a different port number as 3000 since some service is already bound to that port given that error.
Change it in the following line:
app.listen(3000, () => console.log('Example app listening on port 3000!'))
EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.
So in this case, there must be running a service listening on port 3000 already.
you can run this command before start your app
sudo pkill -kill node // to kill any node app running then start your app

I'm trying to deploy a node server to heroku and I'm getting this error: throw er; // Unhandled 'error' event

I'm trying to deploy a node app to heroku and it looks like judging from other questions that heroku is dynamically assigning a port and it is somehow incompatible with something.
(I had to remove some details to post the question)
events.js:182
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:1041:11)
at _exceptionWithHostPort (util.js:1064:20)
at Server.setupListenHandle [as _listen2] (net.js:1305:19)
at listenInCluster (net.js:1370:12)
at Server.listen (net.js:1466:7)
at Function.listen (/app/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/app/index.js:9:21)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! TFP#1.0.0 start: `node index.js`
npm ERR! Failed at the TFP#1.0.0 start script.
Problem
I had the same issue. I ran into this problem when I tried to set the port manually:
app.listen(80, function () {
console.log('Example blog app listening on port 80!')
});
This led me to the same EACCES error.
Solution
I was able to solve the problem by going back and looking through the Heroku docs where I found:
The command in a web process type must bind to the port number specified in the PORT environment variable. If it does not, the dyno will not start.
So, assuming you're using express, if you switch the port assignment to look something like this:
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
Then you application should start properly.
Solution without Express.js
If you are not using Express, then you can set the port simply as:
var PORT = process.env.PORT || 5000;
Where process.env.PORT will be your production port, and 5000 will be the port you would use when testing your server locally.
I have faced this kind of problem. The exact error is the port 80 is already enabled in another project or app. So before start your app, stop the other app mapped to 80 port.
Now you can run your app without this error.
Thanks

port 100 not running in node.js in localhost

I have created a http server in node.js with the following code, and trying to run it on port 100:
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end("Howdy");
}).listen(100);
console.log("server running on port 100");
With this, the server does not start and I am getting the following error message on the linux console:
events.js:72
throw er; // Unhandled 'error' event
^
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:1135:5)
at Object.<anonymous> (/home/badhai/Desktop/mainn.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 Function.Module.runMain (module.js:497:10)
But if I lift a sails.js app on port 100, it runs successfully on port 100. However, the above code runs successfully on port 8081. I want to know if I need to make any changes in the server creation method or elsewhere so that it can be made to run successfully on port 100?
The EACCES part of the error message is the key here - it means you don't have access to that port. Ports < 1024 are system reserved. It's better to use ports in the range 1024-65535
Most modern operating systems limit binding to reserved ports (less than 1024) to processes running as root (or equivalent).
If you absolutely must bind to port 100, googling around will give you a bunch of ways to do it:
https://gist.github.com/firstdoit/6389682
It's better to use ports > 1024. I'm using ports starting from 3000. But if you really want to start it on port 100 and you understand what you're doing then install setcap and just allow to bind ports <1024.
> sudo apt-get install setcap
> sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/node
NodeJS can be installed in other directory also, so better to check where it is and call above setcap command with your path.
> which node
/usr/local/bin/node

New to using NodeJS Express Server

I trying to view a static html page at localhost, but get the following error message:
events.js:72
throw er; // Unhandled 'error' event
^
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:1135:5)
at Function.app.listen (/home/phil/xProgramming/AngJS/Project/node_modules/express/lib/application.js:536:24)
at Object.<anonymous> (/home/phil/xProgramming/AngJS/Project/server.js:6:5)
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)
I've tried killall, but I still get the same error message.
I new to Linux and am using Ubuntu 14.04
The EACCES error normally means that you are trying to bind to a port and failing. This is probably because you are trying to bind to port 80 without root permissions. Try running node with root permissions and it should work.
sudo node server.js
or
sudo nodejs server.js
Depending on how you have Ubuntu setup.
If this is a development environment instead of running as root you should use a port greater than 1024. Typically you will find people using port 3000 for node though 8000 and 8080 are also used. Here is how I normally do this when using Express.
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port);
});

Resources