【问题标题】:How do we specify within a server environment which node.js start script to run?我们如何在服务器环境中指定要运行的 node.js 启动脚本?
【发布时间】:2022-01-15 23:35:47
【问题描述】:

这可能是一个愚蠢的问题,但我很难过。

我有 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 中执行。

我可能想错了,所以请告诉我是否应该以不同的方式处理暂存和生产环境。

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

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

更新

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

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

我是否可以酌情添加类似:npm run start:stagingnpm run start:production 之类的内容?

【问题讨论】:

  • 您的托管环境是什么?
  • 它是 Azure。不是我的选择,顺便说一句,哈哈。

标签: node.js express package.json


【解决方案1】:

你基本上是想运行脚本而不知道要运行的命令,对吧?

所以

npm run start:staging

关于 gh 动作

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

【讨论】:

  • 好的,太棒了。是的,问题主要是我在每个环境中指定哪个脚本为该特定环境运行。在这种情况下,它会在 gh 动作中完成是有道理的,我只是有一个“duh”的时刻。谢谢!
猜你喜欢
  • 2020-08-29
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 2012-03-27
  • 2011-09-24
  • 2020-03-01
  • 2021-10-19
相关资源
最近更新 更多