0
votes

I wanted to host 2 node app on one Linux box from AWS EC2. Where I can run a dev environment and staging environment. I noticed that I will have to use different ports on both of the apps but this is not the problem that I am facing.

Using the staging side which has always worked on the previous boxes that I have started it on to go first, making sure it works before trying out the different ports.

I have a backend node.js using express and a static webpage built with Vue CLI. Where the express app will use app.use(express.static('static')); to host the static webpage. Then proceed with hosting it on PM2 with pm2 start /directoryToDist/main.js. Once the daemon has started, I did a curl http://localhost:80/ but it returns an error HTML page of Cannot GET /.

When I did npm install, npm rebuild and npm build on both apps. Made sure that the dist folder was built properly. Then did a sudo -i and pm2 start /directoryToDist/main.js. Making sure that a node app is running I did a ps -ef | grep js to show that it is running as so. There are no restarts on the pm2 mounted app and everything was running smoothly. I did the curl http://localhost/ after and it did return Cannot GET /.

I did a zip file transfer of a working app in previous boxes and did the necessary installation of npm and run it. Expecting it to work like previous boxes but it didn't. showing the same error of Cannot GET /.

Node js build script

"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./babelrc,./package.json,/.npm-debug-log,./static/* --copy-files"

Vue js build script

"build": "node build/build.js"

Rebuild script that I use

cd api
npm run build
cd ..
cd pwa
npm run build
cd ..
sudo rm -rf api/dist/static
sudo mkdir -p api/dist/static
sudo cp -r pwa/dist/* api/dist/static/
sudo chown -R ubuntu.ubuntu *

Let me know if you need to see more

The actual result is to serve a proper 200 message on the curl before given a domain to the specific port.

2
So the path to your files is located at ROOT/dist/static/*.* ?Matt Oestreich
@MattOestreich yes /home/ubuntu/nodeApp/dist/main.js inside the main.js has a app.use(express.static('static')) that serves index.htmlYikern
I believe it needs to be: app.use(express.static('dist')) in that case.. depending upon where your entry point is located for the Node app..Matt Oestreich
What does your packages.json file look like? Specifically "main"?Matt Oestreich
hmm I have tried it before. Well my nodeApp has a dist folder that has all the built files and then putting the built files of the static nodeApp front end side of it onto static subdirectory of nodeApp's dist and run the main.js app on it.Yikern

2 Answers

1
votes

Or you can find it with path:

import path from 'path'

app.use(
  '/some-url',
  express.static(path.join(__dirname, '/some/path/on/server'))
)

If you know exact path on your server.

Relative paths works too:

app.use(
  '/some-url',
  express.static(path.join(__dirname, '../../some/relative/on/server'))
)

0
votes

Found a solution that has worked.

Pm2 was not pointing to the current working directory. sudo -i and then running it $ pm2 start /home/user/directory will look for for module routes in the directory folder instead of the implemented dist folder.

So by solving this problem. One must traverse through the directory first and initiate the pm2 start there. $ cd /home/user/directory/dist/ $ pm2 start main.js. This has served the node app well and stable.