我们如何在服务器环境中指定 node.js 启动脚本到 运行?

How do we specify within a server environment which node.js start script to run?

这可能是个愚蠢的问题,但我很困惑。

我有 3 个环境:Dev(本地)、Staging(远程)和 Production(远程)。

我在写一个比较简单的Express App。

在我的 package.json 文件中,我为 3 个环境中的每一个都指定了启动脚本。这是完整的文件:

{
  "name": "test-app",
  "version": "0.0.1",
  "description": "test utility app",
  "private": true,
  "scripts": {
    "start": "cross-env NODE_ENV=development nodemon ./bin/www",
    "start:staging": "cross-env NODE_ENV=staging nodemon ./bin/www",
    "start:production": "cross-env NODE_ENV=production && nodemon ./bin/www"
  },
  "devDependencies": {
    "cross-env": "^7.0.3"
  },
  "dependencies": {
    "axios": "^0.24.0",
    "config": "^3.3.6",
    "cookie-parser": "~1.4.4",
    "cors": "^2.8.5",
    "debug": "~2.6.9",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "http-errors": "~1.6.3",
    "jade": "~1.11.0",
    "morgan": "~1.9.1",
    "nodemon": "^2.0.15"
  }
}

现在,我的问题是,如何指定在每个远程服务器环境中应使用哪个启动脚本

我了解如何启动开发环境,所以忽略它并专注于暂存和生产。

例如,我希望 start:staging 在 Staging 中执行,而 start:production 在 Production 中执行。

我可能没有正确考虑这个问题,所以如果我应该以不同的方式处理登台和生产环境,请告诉我。

在我看来,如果没有我以某种方式将它们分配为一个或另一个,登台和生产环境将不知道它们应该作为哪个环境运行。

也许这是作为工作流程的一部分完成的?我正在为 build/deployment 使用 github 操作,仅供参考。

更新

所以,我做了更多的挖掘。在工作流文件中,有一个构建步骤:

 name: npm install, build, and test
        run: |
          npm install
          npm run build --if-present
          npm run test --if-present

我会简单地添加类似的内容吗:npm 运行 start:stagingnpm 运行 start:production 是否合适?

您基本上想要 运行 脚本并且不知道 运行 的命令,对吗?

所以

npm run start:staging

关于 gh-actions

- name: run app on staging
run: npm run start:staging