Unable to start express server on AWS instance

0 votes

I have been searching the whole day the reason of that problem so i've decided to post it:

I can't run my server using PORT=80 npm start

I get the following message :

Port 80 requires elevated privileges
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! server@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the server@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ubuntu/.npm/_logs/2018-03-26T19_52_49_813Z-debug.log
ubuntu@ip-172-31-32-30:~/mean/server$

Here is my code : Package.json

{
  "name": "server",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "prod": "PORT=80 node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.18.2",
    "cookie-parser": "~1.4.3",
    "debug": "~3.1.0",
    "express": "~4.16.2",
    "jade": "~1.11.0",
    "mongoose": "^5.0.1",
    "morgan": "~1.9.0",
    "serve-favicon": "~2.4.5"
  }
}

my app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('morgan');
const mongoose = require('mongoose');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname,'../public')))

mongoose.connect('mongodb://XXXXX:XXXXXX@ds12XXXX.mlab.com:2XXXX/angularXXXX', {}, (err) => {
    if (err) {
        console.log(err);
    } else {
        console.log('db connection ok!');
    }
});
app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, '../public/index.html'))
})


module.exports = app;

I'm using : NPM : 5.6.0 NODE : 8.10.0

Already tried : uninstall modules and npm install again, restart AWS instance, checked if port 80 is already used

Thank you for your help

Oct 1, 2018 in AWS by bug_seeker
• 15,510 points
3,157 views

1 answer to this question.

0 votes

It's not your code — you can't connect to port 80 from an app started with non-root account. You could sudo the node app, but that's not a great practice.

There are a lot of ways to fix this, which you can easily find in a search, but the easiest is to just alter the iptables with something like:

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080

Now you can run your node app on 8080, but reach it through port 80. If you run:

sudo iptables -t nat -L

it will show you something like:

target     prot opt source               destination  
REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:http redir ports 8080

I think this is a pretty common solution if you're just looking for a quick fix.

answered Oct 1, 2018 by Priyaj
• 58,020 points

Related Questions In AWS

0 votes
1 answer

How to install redis on AWS Micro instance?

Maybe you can spin up an EC2 ...READ MORE

answered Jan 30, 2019 in AWS by Archana
• 5,640 points
827 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,946 views
0 votes
1 answer

AWS Lamda with Proxy Integration not recieving request parameters

The lambda proxy integration should pass through ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 58,020 points
953 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,469 views
+3 votes
3 answers

Unable to login to parse dashboard on AWS

During the first time boot, the application ...READ MORE

answered Apr 30, 2018 in AWS by Cloud gunner
• 4,670 points
3,186 views
+1 vote
1 answer

Can we use AWS Public DNS as Domian name to enable https on our instance

I don't believe you can get a ...READ MORE

answered Aug 23, 2018 in AWS by Priyaj
• 58,020 points
991 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP