Why pm2 NodeJS app starts but not accessible on EC2 - node.js

I have a Nodejs app running on AWS EC2. My package.json has the following instructions:
"scripts": {
"build": "babel src -s -D -d dist --presets es2015,stage-0",
"start": "node dist/index.js",
"prestart": "npm run build",
..
So when connected to EC2 I (after install and cd to proj folder) I do PORT=8080 npm start The app starts fine - but messages in the console and is acessinble via my EC2 addres :8080. Also if I run PORT=8080 node dist/index.js
- also good.
But since I would like to use monitoring, restarting of the script by pm2 I try do following:
pm2 start dist/index.js -- PORT=8080
or
PORT=8080 pm2 start dist/index.js
I see that pm2 has the app started,
but it's not acessible on AWS address :8080
What do I do wrong?

Related

Docker container is refusing connection

I am using Dockerfile for Nodejs project but its returning Connection refused error
When I run the app without Docker it works absolutely fine.
Command I use to build and run the docker container is as follows:
docker build -t myapp .
docker run -it -p 8080:8080 myapp
Running above command runs without any error but when I hit http://localhost:8080/test-url it fails
My dockerfile is as follows:
FROM node:16.16.0-alpine
ADD . /opt
COPY . .
RUN npm install
EXPOSE 8080
RUN chmod +x /opt/deploy.sh
RUN apk update && apk add bash
CMD ["/bin/bash", "/opt/deploy.sh"]
And my package.json is as follows (truncated to show only script):
"scripts": {
"start": "DEBUG=app* node index.js",
"build": "rimraf build && babel-node ./src --out-dir build/src && npm run docs",
"dev": "DEBUG=app* nodemon --exec babel-node index.js",
"lint": "eslint 'index.js' 'src/**/*.js' 'src/index.js'",
"docs": "apidoc -i src/ -o public/docs",
"prepare": "husky install",
"lint-staged": "lint-staged"
},
For development I use following command which works fine::
npm run dev
For deploymeent I run deploy.sh which has env variables and final command as ::
npm run build
npm run start
Even when I am trying http://localhost:8080/test-url by loging into docker interactive terminal it returns same error - Connection Refused
Your Port mapping looks right to me. Did you check your firewall? Maybe it is blocking the connection. It could also be helpful to test, if you can run an NGINX-Container on Port 8080. That way you can check if it is a general configuration-problem with Docker or a specific problem of your Image.
Also, did you try to set your node server to listen to 0.0.0.0 instead of localhost? I'm not sure how Docker handles IPs in the Containers, but I was thinking maybe it is called by it's internal ip. If that is the case and your server listens to localhost only, it shouldn't accept the connection.
I hope one of these things can point you in the right direction.

Nodejs deploy on heroku different node (each node has a different port number)

i have different node, here my package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"node_1": "nodemon --watch dev -e js dev/networkNode.js 3001 http://localhost:3001",
"node_2": "nodemon --watch dev -e js dev/networkNode.js 3002 http://localhost:3002",
"node_3": "nodemon --watch dev -e js dev/networkNode.js 3003 http://localhost:3003",
"node_4": "nodemon --watch dev -e js dev/networkNode.js 3004 http://localhost:3004",
"node_5": "nodemon --watch dev -e js dev/networkNode.js 3005 http://localhost:3005"
},
In my local env i run:
npm run node_1
npm run node_2
npm run node_3
npm run node_4
npm run node_5
And the node are accessibile in http://localhost:300X url.
Now i want to deploy on Heroku my prototype.
if i put:
"node_1": "nodemon --watch dev -e js dev/networkNode.js 3001 https://my-heroku-app.herokuapp.com:3001",
And then i go to:
https://my-heroku-app.herokuapp.com:3001
The app doesn't open, but the build is success.
I tried:
Create Procfile:
web: npm run node_1
-Running via Heroku Cli
heroku run npm run node_1
Not works...
How can i set this node on heroku?
What you are trying to do is not possible on Heroku. A deployed App on Heroku is assigned only one port. Furthermore you cannot specify the port. It is supplied to you by Heroku via the environment variable $PORT.
#Tin Nguyen answer is not exact. On Heroku it's mandatory to bind something on $PORT, but then you can bins something else to some other port.
See https://devcenter.heroku.com/articles/dynos#web-dynos
Also see How to run Django and node.js app on same dyno in Heroku? for how to run multiple backends on a dyno and let them communicate.
It makes the case with Django+Node, but it can be used with Node+Node as well.

Run node server with PM2

How could I run node server.js -p by pm2?
Scripts of my package.json is like below,
"scripts": {
"dev": "node server.js",
"start": "node server.js -p"
},
When I execute npm start everything work truly. But I want to run this command with pm2.
To do it when I run pm2 start npm -- start, the process will add to the list of the pm2 but my app not run!
the correct command is
pm2 start server.js
or if you want to pass -p to your app and a name
pm2 start server.js --name "my-server" -- -p

How to start a package.json script in pm2

I want to demonise my express js graphql api server. In windows local dev, I can start my server by running this command and it works fine:
yarn dev
This start command is defined in my package.json like this:
"scripts": {
"dev": "cross-env NODE_ENV=development DEBUG=express:* nodemon --exec babel-node src/index.js"
},
When I try to start this in pm2 in my linux server, I get a success like this:
latheesan#app:~/apps/tweet/server$ pm2 start yarn -- dev
[PM2] Starting /usr/bin/yarn in fork_mode (1 instance)
[PM2] Done.
However, when I type pm2 status it says error and also the display looks really odd:
I am running this on Ubuntu 16.04.
If I don't use the pm2 and just start the app in my ubuntu server with yarn dev - it runs fine.
Any ideas?
Pm2 now support npm
$ pm2 start --interpreter babel-node server.js
(or)
$ pm2 start npm --start
(or)
$ pm2 start npm --name "myAPP" --start
(or)
$ pm2 start npm --name "{app_name}" --run "{script_name}"
I have now resolved this issue.
Install the babel-node globally via: npm install -g babel-cli
Then create the pm2 config in json: pm2.json
{
"apps": [
{
"name": "Tweet GraphQL Server",
"exec_interpreter": "babel-node",
"script": "index.js",
"merge_logs": true,
"cwd": "./src",
"env": {
"NODE_ENV": "production"
}
}
]
}
Now I can run this command to start the pm2 process: pm2 start pm2.json
i do this in my app like this:
in package.json:
"scripts": {
"start": "... start application script ...",
"start:dev": "... start application script as development mode ...",
"pm2": "pm2 start npm --name \"CustomeNameForPM2\" -- run start --watch",
"pm2:dev": "pm2 start npm --name \"CustomeNameForPM2\" -- run start:dev --watch"
}
now you can run pm2 easy with npm run pm2 or npm run pm2:dev
but if you like to do something better, you can read pm2 documentation and use pm2 ecosystem config file

How to Dockerizing a Angular 2 web app via node js?

I tried out finding many ways for Dockerizing angular 2 web app using node js but not yet worked it is running on local but not working on docker container.Does anyone have any proper Dockerfile and package.json file for docking angular 2 app.
FROM node:boron
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 5655
CMD [ "npm","start" ]
Site cant be reached while accessing
Below are some possible ways i found on stack over flow but not worked
docker run --rm --name my-container -it -p 8080:4200 -v $(pwd):/var/www -w "/var/www" node npm start
in package.json even i kept my port as dynamic "ng serve -host 0.0.0.0",
And also suggest me which server i need to use either nginx or node for docking angular 2 web-app
"scripts": {
"start": "node ./bin/www",
"build": "del-cli public/js/app && webpack --config webpack.config.dev.js --progress --profile --watch",
"build:prod": "del-cli public/js/app && ngc -p tsconfig.aot.json && ngc -p tsconfig.aot.json && webpack --config webpack.config.prod.js --progress --profile --bail && del-cli 'public/js/app/**/*.js' 'public/js/app/**/*.js.map' '!public/js/app/bundle.js' '!public/js/app/*.chunk.js' 'assets/app/**/*.ngfactory.ts' 'assets/app/**/*.shim.ts'"
}
first of all I would suggest to run "npm start" at your machine;
and check if after this you can reach you angular app in a browser;
if this works - you will need to remember at which port your angular app is served;
add new RUN section "RUN npm run build:prod" right before EXPOSE line;
set correct port at section EXPOSE;
run you container: "docker run --rm --name my-container -it ."
open browser at http:127.0.0.1:HERE_PORT_FROM_EXPOSE_SECTION
Here is a small example:
https://github.com/karlkori/dockerized-angular-app
Also I want to add that for production it will be better to use Nginx or CloudFront.

Resources