简体   繁体   English

我运行了一个 npm run build,将它放在 nodejs express.static 上,pm2 start app but Cannot GET '/'

[英]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.我想在一个来自 AWS EC2 的 Linux 机器上托管 2 节点应用程序。 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.我有一个使用 express 的后端 node.js 和一个使用 Vue CLI 构建的静态网页。 Where the express app will use app.use(express.static('static'));快速应用程序将使用app.use(express.static('static')); to host the static webpage.托管静态网页。 Then proceed with hosting it on PM2 with pm2 start /directoryToDist/main.js .然后使用pm2 start /directoryToDist/main.js继续在 PM2 上托管它。 Once the daemon has started, I did a curl http://localhost:80/ but it returns an error HTML page of Cannot GET / .一旦守护进程启动,我做了一个curl http://localhost:80/但它返回一个错误的 HTML 页面Cannot GET /

When I did npm install , npm rebuild and npm build on both apps.当我在两个应用程序上执行npm installnpm rebuildnpm build时。 Made sure that the dist folder was built properly.确保正确构建了dist文件夹。 Then did a sudo -i and pm2 start /directoryToDist/main.js .然后做了一个sudo -ipm2 start /directoryToDist/main.js Making sure that a node app is running I did a ps -ef | grep js确保节点应用程序正在运行我做了一个ps -ef | grep js ps -ef | grep js to show that it is running as so. ps -ef | grep js显示它正在运行。 There are no restarts on the pm2 mounted app and everything was running smoothly. pm2 安装的应用程序没有重新启动,一切运行顺利。 I did the curl http://localhost/ after and it did return Cannot GET / .我做了curl http://localhost/之后,它确实返回了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.我在以前的盒子中做了一个工作应用程序的 zip 文件传输,并做了必要的 npm 安装并运行它。 Expecting it to work like previous boxes but it didn't.期望它像以前的盒子一样工作,但事实并非如此。 showing the same error of Cannot GET / .显示Cannot GET /的相同错误。

Node js build script Node.js 构建脚本

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

Vue js build script Vue js 构建脚本

"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.实际结果是在将域分配给特定端口之前,在 curl 上提供正确的200消息。

Or you can find it with path :或者你可以用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'))
)

Found a solution that has worked.找到了一个有效的解决方案。

Pm2 was not pointing to the current working directory. Pm2 未指向当前工作目录。 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. sudo -i然后运行它$ pm2 start /home/user/directory将在directory文件夹而不是已实现的dist文件夹中查找模块路由。

So by solving this problem.所以通过解决这个问题。 One must traverse through the directory first and initiate the pm2 start there.必须先遍历目录并在那里启动 pm2 start。 $ cd /home/user/directory/dist/ $ pm2 start main.js . $ cd /home/user/directory/dist/ $ pm2 start main.js This has served the node app well and stable.这为节点应用程序提供了良好且稳定的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM