Nodejs Express on Heroku App EACCES 0.0.0.0:80 - javascript

I am trying to run a Node app on a newly created Heroku App from their web. I followed their steps and still I am getting errors when displaying app status.
I followed the Node.js getting started section without the heroku create command since I already created from the web.
So when I run: heroku ps:scale web=1 it shows me:
Scaling dynos... done, now running web at 1:Free
But when running heroku ps:
=== web (Free): npm run start (1)
web.1: crashed 2018/10/25 11:25:49 -0300 (~ 8m ago)
So my logs on heroku logs --tail show me this error:
2018-10-25T14:25:44.000000+00:00 app[api]: Build succeeded
2018-10-25T14:25:46.451739+00:00 heroku[web.1]: Starting process with command npm run start
2018-10-25T14:25:49.113832+00:00 app[web.1]:
2018-10-25T14:25:49.113864+00:00 app[web.1]: > my-app#1.0.0 start /app
2018-10-25T14:25:49.113866+00:00 app[web.1]: > node server.js
2018-10-25T14:25:49.113867+00:00 app[web.1]:
2018-10-25T14:25:49.418151+00:00 app[web.1]: events.js:167
2018-10-25T14:25:49.418191+00:00 app[web.1]: throw er; // Unhandled 'error' event
2018-10-25T14:25:49.418193+00:00 app[web.1]: ^
2018-10-25T14:25:49.418194+00:00 app[web.1]:
2018-10-25T14:25:49.418196+00:00 app[web.1]: Error: listen EACCES 0.0.0.0:80
So I checked if I made a mistake while setting all up.
I use a simple Express routing and server with this code:
app.get('/', (req, res) => { ... });
app.listen(80, err => {
if(err) throw err;
console.log("%c Server running", "color: green");
});
Also I made sure I added engines to package.json:
"scripts": {
"start": "node server.js"
},
"engines": {
"node": "10.11.0",
"npm": "6.4.1"
},
I've also created a Procfile file in the root path with web: npm run start inside.
So when everything is checked I just run these commands and everything looks great until I check the logs or visit the app:
git commit -am "my commit text"
git push heroku master
I see this in the logs:
remote: -----> Compressing...
remote: Done: 18.3M
remote: -----> Launching...
remote: Released v12
remote: https://my-app.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/my-app.git
3656da0..f1eb078 master -> master
So... Any suggestions on what am I doing wrong? Thanks in advance.

You need to provide port for app to listen on in environment variable.
What heroku does is it runs our app on dynamic port.
Try using this:
const PORT = process.env.PORT || 3000;
app.listen(PORT, err => {
if(err) throw err;
console.log("%c Server running", "color: green");
});
Also you shouldn't bind the port to 80, as it is a reserved standard
http port.

Heroku dynos expose a dynamic port for your app to bind to. This value is exposed in the $PORT env var. You must change your code to bind to this port instead.
const port = process.env.PORT || 3000;
app.listen(port, err => {
if(err) throw err;
console.log("%c Server running", "color: green");
});

Related

How to run node js application without terminal command

I'm so new in node.js and I have a simple node.js project with only one js file (vpn.js) which use a module and an index.html which opens using a function in vpn.js. I have installed this package and the require function can find its module. I have a vpn.js file and an index.html (in index.html I only have a video tag with a src.). Now my question is should I always run my code with terminal? how should I host this project? Basically no clients can run terminal commands on web. (note: I'm using Windows not Linux)
this is the code of my js file:
const openvpnmanager = require('node-openvpn');
const opts = {
host: '192.168.1.7', // normally '127.0.0.1', will default to if undefined
port: 8080, //port openvpn management console
timeout: 1500, //timeout for connection - optional, will default to 1500ms if undefined
logpath: 'log.txt' //optional write openvpn console output to file, can be relative path or absolute
};
const auth = {
user: 'vpnUserName',
pass: 'vpnPassword',
};
const openvpn = openvpnmanager.connect(opts)
// will be emited on successful interfacing with openvpn instance
openvpn.on('connected', () => {
//openvpnmanager.authorize(auth);
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('index.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(5500);
});
// emits console output of openvpn instance as a string
openvpn.on('console-output', output => {
console.log(output);
});
// emits console output of openvpn state as a array
openvpn.on('state-change', state => {
console.log(state)
});
// emits console output of openvpn state as a string
openvpn.on('error', error => {
console.log(error)
});
Use pkg npm package. This will create an executable file for your nodejs project. You can create executable file for Windows or mac or linux.
Install pkg globally using following command
npm install -g pkg
After installing it, use:
pkg app.js[entry file to your project] to create executable file.
For more info about pkg, look into pkg
you can done it help of set autorun the node server forever.
here is some step
You may also want to consider using the upstart utility. It will allow you to start, stop and restart you node application like a service. Upstart can configured to automatically restart your application if it crashes.
Install upstart:
sudo apt-get install upstart
Create a simple script for your application that will look something like:
#!upstart
description "my app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/app.js >> /var/log/myapp.log 2>&1
Then copy the script file (myapp.conf) to /etc/init and make sure its marked as executable. Your application can then be managed using the following commands:
sudo start myapp
sudo stop myapp
sudo restart myapp

Running json-server in docker

I've been running json-server in a docker container for a while successfully with the following config:
Docker File:
FROM node:alpine
EXPOSE 3000
COPY deploy/conf/mockBackend.json /opt/mockBackend.json
RUN yarn global add json-server
CMD ["json-server", "/opt/mockBackend.json"]
Run command:
docker run -d -p 3000:3000 --name mockbackend mockbackend
Recently this stopped working. I get ERR_CONNECTION_REFUSED
in the browser.
By default json-server binds to 127.0.0.1, which could cause this issue if this is correct.
I don't know why it worked before, but binding json server to every interface with json-server -H 0.0.0.0 mockBackend.json makes it work again.
My problem now is that starting json-server that way in the image manually works fine, but when I change the CMD line of the Dockerfile to CMD ["json-server", "-H 0.0.0.0", "/opt/mockBackend.json"] I get the folloing error:
Docker log:
\{^_^}/ hi!
Loading /opt/mockBackend.json
Done
Resources
http:// 0.0.0.0:3000/xxx
http:// 0.0.0.0:3000/yyy
Home
http:// 0.0.0.0:3000
Type s + enter at any time to create a snapshot of the database
events.js:167
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND 0.0.0.0
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:50:26)
Emitted 'error' event at:
at GetAddrInfoReqWrap.doListen [as callback] (net.js:1498:12)
It looks like the json-server is starting fine, but then exits with the error. Any idea why this only happens when starting the container with the CMD line, but not when running the same json-server command manually inside the same image?

How do I get SocketCluster to restart on file change?

I'm running http://socketcluster.io/ and I want to restart my workers whewnever a file changes. However, nodemon server.js fails as soon as it tries to restart with an endlessly-repeating wall of:
1445633138359 - Origin: Worker (PID 44372)
[Error] Error: connect ECONNREFUSED /var/folders/fj/yzfd3_tn7xd0smz7j6s093mh0000gn/T/socketcluster/6879fe94-ed92-4188-b1d7-cb187a5ade4e_b68fcded6c/b0
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
1445633138362 - Worker 0 exited - Exit code: 0
How can I safely restart SocketCluster to load the new changes?
nodemon sends the SIGUSR2 signal to the main (Master) process. SocketCluster (correctly) interprets this as as a request to reboot the workers. Unfortunately, there's an open issue where things are not shut down properly and errors fly all around.
There are two options:
You can add the code from the linked issue:
house.addShutdownHandler(function(ctx, next){
socketCluster.killWorkers();
socketCluster.killBrokers();
next();
});
or use forever to send a "restart everything" signal:
forever -w --killSignal=SIGTERM server.js
Improvements were made for nodemon in SC version 5.0.23 or later.
Make sure that you pass killMasterOnSignal: true when instantiating SocketCluster in your code (server.js file) - This setting is necessary for nodemon to work.

How to run openshift MEAN project in localhost or development system Ubuntu?

I have created a MEAN stack application on https://openshift.redhat.com/ and it was successfully running on the web.
Default source code of the project synced form: https://github.com/linnovate/mean-on-openshift.git
For further development I am cloning the hosted git source using using git url mentioned in the applications page. (e.g git clone ssh://55e86e9f89f5cf1d29000001#nodejs-tapslab.rhcloud.com/~/git/nodejs.git/).
After clone the source code to local system, opening the project in sublime text editor. I can see in the config/env/development.js there are lot of configuration are used like process.env.OPENSHIFT_XXX. I was changed the some config value mentioned below and trying to start server using command : 'node server', but server was not starting throwing error.
{
db: db: 'mongodb://' + (process.env.DB_PORT_27017_TCP_ADDR || 'localhost:27017') + ':' + '/mean-dev',
hostname: process.env.OPENSHIFT_NODEJS_IP || 'http://localhost:3000'
}
Can someone help me how to run Openshift mean project in local system and what all configuration needed before running it in local system.
System Config :
Ubuntu 12.04
MongoDB v2.6.6
Nodejs v0.10.40
npm 1.4.28
grunt-cli v0.1.13
Error :
$ node server
Mean app started on port 3000 (development)
events.js:72
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
Git:
$ NODE_ENV=test node server

Unhandled 'error' event while trying to run mean stack

I follower every instruction for installing a MEAN stack application.
At the end of it all I run grunt and when I go to http://localhost:3000 I get the following error:
Running "clean:0" (clean) task
Running "jshint:all" (jshint) task
>> 47 files lint free.
Running "csslint:src" (csslint) task
>> 5 files lint free.
Running "concurrent:tasks" (concurrent) task
Running "nodemon:dev" (nodemon) task
Running "watch" task
Waiting...
[nodemon] v1.0.20
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node --debug server.js`
debugger listening on port 5858
Mongoose: packages.findOne({ name: 'config' }) { fields: undefined }
3000
Mean app started on port 3000 (development)
Mongoose: users.ensureIndex({ email: 1 }) { safe: undefined, background: true, unique: true }
Mongoose: users.ensureIndex({ username: 1 }) { safe: undefined, background: true, unique: true }
GET / 304 41.185 ms - -
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, open '/var/www/finnviz/bower_components/bootstrap/dist/css/bootstrap.css'
[nodemon] app crashed - waiting for file changes before starting...
I searched and found this answer, but this does not seem to be the case, when I run ps aux | grep node I dont get any running process:
trufa 14763 0.0 0.0 23492 956 pts/14 S+ 15:39 0:00 grep --color=auto node
I searched through many answers but none seem to apply. Some suggest to change the port. I tried it and the error is the same.
Any suggestions as to how to proceed?
Sometimes the automatic bower install does not work if you run npm with sudo.
I would recommend setting up npm so it does not require sudo.
Try running $ bower install from your project root, and then restart the server.
you had run another server using the same port 8080.

Categories

Resources