0

I can't get Express working.now. I get the following error :

server.js
events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES 0.0.0.0:80
    at Object.exports._errnoException (util.js:856:11)
    at exports._exceptionWithHostPort (util.js:879:20)
    at Server._listen2 (net.js:1214:19)
    at listen (net.js:1263:10)
    at Server.listen (net.js:1359:5)
    at EventEmitter.listen (D:\Users\ME\Desktop\Utile\Web\_NodeJS\node_modules\express\lib\application.js:617:24)
    at Object.<anonymous> (D:\Users\ME\Desktop\Utile\Web\_NodeJS\www.njstesting.herokuapp.com\server.js:20:5)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)

I have the following structure :

-server.js
-public
--index.html
--css
---index_styles.css

With the following server.js code :

var express = require('express');
var path = require('path');

var app = express();

app.use(express.static(__dirname + '/public'));
app.use('/static', express.static(__dirname + '/public'));

app.get('/', function(request, response) {
    res.sendFile('/index.html')
});

app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

var port = process.env.PORT || 80;
app.listen(port, function() {
    console.log("Node app is running at localhost:" + port);
});

Moreover, I have this index.html code that have already maked problems

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="css\index_style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <style>
        body {
            background-color: #F5F5F5;
        }
        .father {
            margin-left: 20px;
            margin-right: 20px;
        }
        .sinput {
            role: textbox;
            border: 2px solid #444;
            padding: 5px 5px;
            background-color: #FFFFFF;
        }
    </style>
</head>

So, I want to run the server and get if possible the html file (index for root http://domain.com) along with the css and javascripts files. It also should be compatible with Heroku PaaS.

Thank you !

2 Answers2

1

Running on Heroku

When running your apps on Heroku you have to use the port as specified in the PORT environment variable.

See http://devcenter.heroku.com/articles/node-js

var port = process.env.PORT || 3000;

app.listen(port, function() {
  console.log('Listening on ' + port);
});
halfer
  • 19,824
  • 17
  • 99
  • 186
Amulya Kashyap
  • 2,333
  • 17
  • 25
  • Thank you, now it runs! :) However, the CSS files don't load, any ideas? – Patrick Fenster Mar 13 '16 at 09:59
  • Ok it was just a typo somewhere. Why port 3000 works, and not 80? – Patrick Fenster Mar 13 '16 at 14:07
  • because it may happen that port 80 | 8080 might be used by some other app at time of your's app execution. For example. team viewer use port 8080 on my computer hence i cannot make any app run on 8080 till team viewer is running... that's why – Amulya Kashyap Mar 14 '16 at 03:44
0

I know this question is old and the OP doesn't need an answer, but just in case it help others...

I ran into this and in my case it was simply because port 80 is a privileged port and I wasn't root. For those that don't know, the first 1024 TCP ports are privileged which means any process that starts them does so with root privileges. This is intended to prevent rogues from starting alternative/fake services such as SSH on your server if it became compromised. If port 23 is available you can be sure it was started by someone with root access.

'sudo node app.js' should do the trick.

Resources:
Why are ports below 1024 privileged? https://www.w3.org/Daemon/User/Installation/PrivilegedPorts.html https://www.staldal.nu/tech/2007/10/31/why-can-only-root-listen-to-ports-below-1024/

Community
  • 1
  • 1
McFly
  • 705
  • 6
  • 9