在 Azure 应用程序服务上使用 Express 创建反应应用程序无法启动

create react app with express on azure app service fails to start

我已经部署到 Azure create-react-app,它有一个 server.js 文件用于动态生成 meta tags

堆栈是 Linux

上的节点 16

在我的启动命令中我有 pm2 start /home/site/wwwroot/server.js --no-daemon

我的server.js如下

const express = require("express");
const path = require("path");
const fs = require("fs");

const PORT = process.env.PORT || 5000;

const app = express();

app.get("/", (req, res) => {
  const filePath = path.resolve(__dirname, "index.html");
  fs.readFile(filePath, "utf8", (err, data) => {
    if (err) {
      return console.log(err);
    }

    data = data.replace(/__META_OG_TITLE__/g, "Home Page").replace(/__META_DESCRIPTION__/g, "Home page description.");

    res.send(data);
  });
});

app.get("/snp/list", (req, res) => {
  const filePath = path.resolve(__dirname, "index.html");
  fs.readFile(filePath, "utf8", (err, data) => {
    if (err) {
      return console.log(err);
    }

    data = data.replace(/__META_OG_TITLE__/g, "SNP List").replace(/__META_DESCRIPTION__/g, "SNP List description.");

    res.send(data);
  });
});

app.get("/snp/detail", (req, res) => {
  const filePath = path.resolve(__dirname, "index.html");
  fs.readFile(filePath, "utf8", (err, data) => {
    if (err) {
      return console.log(err);
    }

    data = data.replace(/__META_OG_TITLE__/g, "SNP Detail").replace(/__META_DESCRIPTION__/g, "SNP detail description.");

    res.send(data);
  });
});

app.use(express.static(path.resolve(__dirname)));

app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});

server.jsindex.html 的兄弟姐妹。

我收到 Cannot find module 'express' 错误,这是我的错误日志转储

 2022-02-23T22:12:16.154879649Z NodeJS Version : v16.6.1
2022-02-23T22:12:16.154883449Z Note: Any data outside '/home' is not persisted
2022-02-23T22:12:16.154887149Z 
2022-02-23T22:12:16.491995689Z Cound not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
2022-02-23T22:12:16.492031589Z Could not find operation ID in manifest. Generating an operation id...
2022-02-23T22:12:16.492036389Z Build Operation ID: 8e9bf4f8-2b72-4d38-ad94-8e3461d1caed
2022-02-23T22:12:17.563270289Z Environment Variables for Application Insight's IPA Codeless Configuration exists..
2022-02-23T22:12:17.626540290Z Writing output script to '/opt/startup/startup.sh'
2022-02-23T22:12:17.864719008Z Running #!/bin/sh
2022-02-23T22:12:17.865929003Z 
2022-02-23T22:12:17.865970803Z # Enter the source directory to make sure the script runs where the user expects
2022-02-23T22:12:17.866221302Z cd "/home/site/wwwroot"
2022-02-23T22:12:17.866787300Z 
2022-02-23T22:12:17.866803800Z export NODE_PATH=/usr/local/lib/node_modules:$NODE_PATH
2022-02-23T22:12:17.866809300Z if [ -z "$PORT" ]; then
2022-02-23T22:12:17.867117999Z      export PORT=8080
2022-02-23T22:12:17.867133499Z fi
2022-02-23T22:12:17.867138499Z 
2022-02-23T22:12:17.868571094Z PATH="$PATH:/home/site/wwwroot" pm2 start /home/site/wwwroot/server.js --no-daemon
2022-02-23T22:12:18.222455804Z 
2022-02-23T22:12:18.222498404Z                         -------------
2022-02-23T22:12:18.222506204Z 
2022-02-23T22:12:18.222510804Z __/\\\\\\\____/\\____________/\\____/\\\\\_____
2022-02-23T22:12:18.222516104Z  _\/\\/////////\\_\/\\\________/\\\__/\\///////\\___
2022-02-23T22:12:18.222521604Z   _\/\\_______\/\\_\/\\//\\____/\\//\\_\///______\//\\__
2022-02-23T22:12:18.222526904Z    _\/\\\\\\\/__\/\\///\\/\\/_\/\\___________/\\/___
2022-02-23T22:12:18.222533104Z     _\/\\/////////____\/\\__\///\\/___\/\\________/\\//_____
2022-02-23T22:12:18.222550803Z      _\/\\_____________\/\\____\///_____\/\\_____/\\//________
2022-02-23T22:12:18.222555903Z       _\/\\_____________\/\\_____________\/\\___/\\/___________
2022-02-23T22:12:18.222560103Z        _\/\\_____________\/\\_____________\/\\__/\\\\\\\\_
2022-02-23T22:12:18.222564603Z         _\///______________\///______________\///__\///////////////__
2022-02-23T22:12:18.222568703Z 
2022-02-23T22:12:18.222572503Z 
2022-02-23T22:12:18.222576203Z                           Runtime Edition
2022-02-23T22:12:18.222580003Z 
2022-02-23T22:12:18.222583703Z         PM2 is a Production Process Manager for Node.js applications
2022-02-23T22:12:18.222587503Z                      with a built-in Load Balancer.
2022-02-23T22:12:18.222591303Z 
2022-02-23T22:12:18.222595103Z                 Start and Daemonize any application:
2022-02-23T22:12:18.222598903Z                 $ pm2 start app.js
2022-02-23T22:12:18.222602603Z 
2022-02-23T22:12:18.222606303Z                 Load Balance 4 instances of api.js:
2022-02-23T22:12:18.222610003Z                 $ pm2 start api.js -i 4
2022-02-23T22:12:18.222613803Z 
2022-02-23T22:12:18.222619703Z                 Monitor in production:
2022-02-23T22:12:18.222623703Z                 $ pm2 monitor
2022-02-23T22:12:18.222627503Z 
2022-02-23T22:12:18.222631203Z                 Make pm2 auto-boot at server restart:
2022-02-23T22:12:18.222635003Z                 $ pm2 startup
2022-02-23T22:12:18.222638703Z 
2022-02-23T22:12:18.222642403Z                 To go further checkout:
2022-02-23T22:12:18.222646103Z                 http://pm2.io/
2022-02-23T22:12:18.222649803Z 
2022-02-23T22:12:18.222653603Z 
2022-02-23T22:12:18.222657303Z                         -------------
2022-02-23T22:12:18.222661103Z 
2022-02-23T22:12:18.231376071Z pm2 launched in no-daemon mode (you can add DEBUG="*" env variable to get more messages)
2022-02-23T22:12:18.447267984Z 2022-02-23T22:12:18: PM2 log: Launching in no daemon mode
2022-02-23T22:12:18.481629859Z 2022-02-23T22:12:18: PM2 log: [PM2] Starting /home/site/wwwroot/server.js in fork_mode (1 instance)
2022-02-23T22:12:18.486247242Z 2022-02-23T22:12:18: PM2 log: App [server:0] starting in -fork mode-
2022-02-23T22:12:18.515573535Z 2022-02-23T22:12:18: PM2 log: App [server:0] online
2022-02-23T22:12:18.529572684Z 2022-02-23T22:12:18: PM2 log: [PM2] Done.
2022-02-23T22:12:18.632207310Z 2022-02-23T22:12:18: PM2 log: ┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
2022-02-23T22:12:18.632269910Z │ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
2022-02-23T22:12:18.632279110Z ├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
2022-02-23T22:12:18.632284510Z │ 0   │ server    │ default     │ N/A     │ fork    │ 39       │ 0s     │ 0    │ online    │ 0%       │ 30.6mb   │ root     │ disabled │
2022-02-23T22:12:18.632289010Z └─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
2022-02-23T22:12:18.638526987Z 2022-02-23T22:12:18: PM2 log: [--no-daemon] Continue to stream logs
2022-02-23T22:12:18.639576183Z 2022-02-23T22:12:18: PM2 log: [--no-daemon] Exit on target PM2 exit pid=28
2022-02-23T22:12:18.763260232Z 2022-02-23T22:12:18: PM2 log: [PM2] This PM2 is not UP TO DATE
2022-02-23T22:12:18.766529521Z 2022-02-23T22:12:18: PM2 log: [PM2] Upgrade to version 5.2.0
2022-02-23T22:12:18.769252311Z 22:12:18 PM2      | [PM2] This PM2 is not UP TO DATE
2022-02-23T22:12:18.770937304Z 22:12:18 PM2      | [PM2] Upgrade to version 5.2.0
2022-02-23T22:12:18.789006939Z 22:12:18 0|server | Error: Cannot find module 'express'
2022-02-23T22:12:18.790186234Z 22:12:18 0|server | Require stack:
2022-02-23T22:12:18.791370430Z 22:12:18 0|server | - /home/site/wwwroot/server.js
2022-02-23T22:12:18.793005124Z 22:12:18 0|server |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-02-23T22:12:18.794990917Z 22:12:18 0|server |     at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:61:29)
2022-02-23T22:12:18.799204201Z 22:12:18 0|server |     at require (node:internal/modules/cjs/helpers:94:18)
2022-02-23T22:12:18.800213398Z 22:12:18 0|server |     at Object.<anonymous> (/home/site/wwwroot/server.js:1:17)
2022-02-23T22:12:18.801263394Z 22:12:18 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14)
2022-02-23T22:12:18.802277190Z 22:12:18 0|server |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
2022-02-23T22:12:18.803243187Z 22:12:18 0|server |     at Module.load (node:internal/modules/cjs/loader:981:32)
2022-02-23T22:12:18.804699981Z 22:12:18 0|server |     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-02-23T22:12:18.805678378Z 22:12:18 0|server |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
2022-02-23T22:12:18.806396875Z 22:12:18 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14) {
2022-02-23T22:12:18.807571271Z 22:12:18 0|server |   code: 'MODULE_NOT_FOUND',
2022-02-23T22:12:18.807588071Z 22:12:18 0|server |   requireStack: [ '/home/site/wwwroot/server.js' ]
2022-02-23T22:12:18.810564360Z 22:12:18 0|server | }
2022-02-23T22:12:18.810584860Z 2022-02-23T22:12:18: PM2 log: App [server:0] exited with code [1] via signal [SIGINT]
2022-02-23T22:12:18.814429746Z 22:12:18 PM2      | App [server:0] exited with code [1] via signal [SIGINT]
2022-02-23T22:12:18.816066740Z 2022-02-23T22:12:18: PM2 log: App [server:0] starting in -fork mode-
2022-02-23T22:12:18.817591534Z 22:12:18 PM2      | App [server:0] starting in -fork mode-
2022-02-23T22:12:18.837398662Z 2022-02-23T22:12:18: PM2 log: App [server:0] online
2022-02-23T22:12:18.839963853Z 22:12:18 PM2      | App [server:0] online
2022-02-23T22:12:19.077033189Z 22:12:19 0|server | Error: Cannot find module 'express'
2022-02-23T22:12:19.078228284Z 22:12:19 0|server | Require stack:
2022-02-23T22:12:19.079877278Z 22:12:19 0|server | - /home/site/wwwroot/server.js
2022-02-23T22:12:19.081284973Z 22:12:19 0|server |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-02-23T22:12:19.081781471Z 22:12:19 0|server |     at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:61:29)
2022-02-23T22:12:19.082940467Z 22:12:19 0|server |     at require (node:internal/modules/cjs/helpers:94:18)
2022-02-23T22:12:19.084115863Z 22:12:19 0|server |     at Object.<anonymous> (/home/site/wwwroot/server.js:1:17)
2022-02-23T22:12:19.085170459Z 22:12:19 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14)
2022-02-23T22:12:19.086312955Z 22:12:19 0|server |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
2022-02-23T22:12:19.089914542Z 22:12:19 0|server |     at Module.load (node:internal/modules/cjs/loader:981:32)
2022-02-23T22:12:19.090947538Z 22:12:19 0|server |     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-02-23T22:12:19.091896934Z 22:12:19 0|server |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
2022-02-23T22:12:19.093072430Z 22:12:19 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14) {
2022-02-23T22:12:19.096341718Z 22:12:19 0|server |   code: 'MODULE_NOT_FOUND',
2022-02-23T22:12:19.097369815Z 22:12:19 0|server |   requireStack: [ '/home/site/wwwroot/server.js' ]
2022-02-23T22:12:19.099276608Z 22:12:19 0|server | }
2022-02-23T22:12:19.100614503Z 2022-02-23T22:12:19: PM2 log: App [server:0] exited with code [1] via signal [SIGINT]
2022-02-23T22:12:19.104935187Z 22:12:19 PM2      | App [server:0] exited with code [1] via signal [SIGINT]
2022-02-23T22:12:19.106340982Z 2022-02-23T22:12:19: PM2 log: App [server:0] starting in -fork mode-
2022-02-23T22:12:19.107983276Z 22:12:19 PM2      | App [server:0] starting in -fork mode-
2022-02-23T22:12:19.122067624Z 2022-02-23T22:12:19: PM2 log: App [server:0] online
2022-02-23T22:12:19.124422816Z 22:12:19 PM2      | App [server:0] online
2022-02-23T22:12:19.342399121Z 22:12:19 0|server | Error: Cannot find module 'express'
2022-02-23T22:12:19.344263414Z 22:12:19 0|server | Require stack:
2022-02-23T22:12:19.346447807Z 22:12:19 0|server | - /home/site/wwwroot/server.js
2022-02-23T22:12:19.347436203Z 22:12:19 0|server |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-02-23T22:12:19.351468388Z 22:12:19 0|server |     at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:61:29)
2022-02-23T22:12:19.352447085Z 22:12:19 0|server |     at require (node:internal/modules/cjs/helpers:94:18)
2022-02-23T22:12:19.353268082Z 22:12:19 0|server |     at Object.<anonymous> (/home/site/wwwroot/server.js:1:17)
2022-02-23T22:12:19.354038979Z 22:12:19 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14)
2022-02-23T22:12:19.354565177Z 22:12:19 0|server |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
2022-02-23T22:12:19.355181375Z 22:12:19 0|server |     at Module.load (node:internal/modules/cjs/loader:981:32)
2022-02-23T22:12:19.356654469Z 22:12:19 0|server |     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-02-23T22:12:19.357570966Z 22:12:19 0|server |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
2022-02-23T22:12:19.360012257Z 22:12:19 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14) {
2022-02-23T22:12:19.360040657Z 22:12:19 0|server |   code: 'MODULE_NOT_FOUND',
2022-02-23T22:12:19.360920954Z 22:12:19 0|server |   requireStack: [ '/home/site/wwwroot/server.js' ]
2022-02-23T22:12:19.362052350Z 22:12:19 0|server | }

如果我将启动命令切换为 pm2 serve /home/site/wwwroot --no-daemon --spa 那么一切正常。

并且,如果我发出 node server.js 从构建文件夹本地,那么它再次在 http://localhost:5000/

下完美运行

我花了一整天的时间试图找出可能出了什么问题,任何帮助将不胜感激。

编辑

现在我只是 ftp 将我的构建文件夹中的所有文件发送到 site/wwwroot。在这一点上,我并不担心设置 CI/CD 链,因为我只是首先希望该应用程序 运行.

正在 settings/configuration/general settings/startup 命令下的 Azure 门户中设置启动命令

EDIT-2 我通过 npm install pm2 -g 在本地机器上安装了 pm2,如果我发出命令 pm2 start server.js 然后从构建文件夹安装了 pm2,那么应用程序会成功启动。

我的本地节点版本是v14.5.0

  • 当您为 React、Angular 或其他 Node 框架创建生产构建时,文件将放置在 builddist 目录(文件夹)中,具体取决于框架。
  • Linux 上的应用服务使用 Oryx 来检测、构建和启动您的应用程序。
  • 使用--spa会自动将所有查询重定向到index.html_
pm2 serve /home/site/wwwroot --no-daemon --spa
  • 为了提供构建的内容,您可以执行以下任一操作:

PM2 服务

  1. 如果 build 目录中的所有项目都在 wwwroot 中,请将路径更改为 /home/site/wwwroot.

    pm2 serve /home/site/wwwroot/build --no-daemon    
    
  2. 如果内容在 dist 文件夹下,请确保使用

    pm2 serve /home/site/wwwroot/dist --no-daemon
    

处理文件

  • 创建一个 process.json 或 process.yml ,将其放在 /home/site/wwwroot.

       {
         "script": "serve",
         "env": {
             "PM2_SERVE_PATH": './build'
         }
         "args": '--no-daemon'
         }    
    
  • 在 Azure 门户中,转到 配置.=> 常规 并找到 启动命令框并输入process.json =>保存。

更多信息请参考Configure Node.js server , deploy React application in Azure App Service and Using PM2 on App Service Linux

问题的解决方案就在眼前。

我所要做的就是 SSHhome/site/wwwroot & npm install express

一切都与我的 meta tags 动态生成完美配合。