Skip to content
Advertisement

I ran a npm run build, placed it on nodejs express.static, pm2 start app but Cannot GET ‘/’

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement