【问题标题】:how to deploy ExpressJS in TypeScript to Azure?如何将 TypeScript 中的 ExpressJS 部署到 Azure?
【发布时间】:2021-06-24 08:36:30
【问题描述】:

我正在尝试使用此快速生成器为 API 服务创建快速服务器:
generator-express-no-stress-typescript

我可以使用 npm run dev 在本地运行 Express 服务器。
我可以运行 npm run compile 在 dist 文件夹中创建生产版本。
dist 文件夹产品包可以使用npm run start 启动,没有问题。

但是,我未能将应用程序部署到我的 Azure 服务器。我已关注this Microsoft document,使用 Visual Studio Code 将 Express.js 部署到 Azure App Service。完成所有步骤后,当我浏览应用程序时,它显示You do not have permission to view this directory or page.

我可以参考任何文章或文件吗?感谢您的帮助。

package.json scripts 部分默认由generator-express-no-stress-typescript创建

  "scripts": {
    "start": "node dist/index.js",
    "compile": "ts-node build.ts && tsc",
    "dev": "nodemon server/index.ts | pino-pretty",
    "dev:debug": "nodemon --exec \"node -r ts-node/register --inspect-brk\" server/index.ts | pino-pretty",
    "lint": "eslint -c .eslintrc.js \"{server, test}/**/*.{js,ts,tsx}\" --quiet",
    "lint:fix": "eslint -c .eslintrc.js \"{server, test}/**/*.{js,ts,tsx}\" --quiet --fix",
    "test": "mocha -r ts-node/register test/**/*.ts --exit",
    "test:debug": "mocha -r ts-node/register --inspect-brk test/**/*.ts --exit"
  },

【问题讨论】:

  • Linux 还是 Windows ?
  • 我的电脑是 Windows,在 Azure 上我也选择了 Windows。
  • 请运行'tsc -v',然后告诉我你使用的是哪个打字稿版本,因为我无法通过在本地运行'npm run start'来成功运行。我可以运行“npm run start:dev”。
  • 我的打字稿版本是 4.3.2
  • 经过一系列的测试,我失败了。如果有其他相关经验的人可以提供答案,我将开始赏金。

标签: node.js typescript azure express web-deployment


【解决方案1】:

我终于弄清楚我的包和部署出了什么问题。在这里我将分享我的发现。

Azure 需要 iisnode/web.config 来执行应用程序

当我再次查看this Microsoft tutorial document时,我发现在他们的示例项目中,根文件夹下有一个web.config文件

参考此链接:https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config

它说“如果iisnode用于运行节点进程,则需要此配置文件”

我将相同的文件复制到我的项目中,我的项目是由这个生成器生成的:generator-express-no-stress-typescript
我对我的项目做了什么:

  1. 使用生成器生成示例 Express API Server
  2. 运行npm run compile 将产品构建编译到dist 文件夹中
  3. 将上面的 web.config 复制到根文件夹中
  4. 修改内容,路径指向index.js,改为dist/index.js
    <handlers>
      <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
      <add name="iisnode" path="dist/index.js" verb="*" modules="iisnode"/>
    </handlers>
    ......
        <rule name="DynamicContent">
          ......
          <action type="Rewrite" url="dist/index.js"/>
        </rule>

毕竟,我再次将应用程序部署到 Azure,现在日志文件错误消息已更改。现在它显示类似 "Invalid port number, should be in range 0 - 65xxx"

修复列表端口逻辑

看来Azure端口号环境不是一个数字,它是一个管道字符串: https://www.geekwithopinions.com/2013/12/09/node-js-and-azure-is-it-a-port-or-a-pipe/

但是,生成的index.ts逻辑,由于是TypeScript,逻辑parseInt为端口,导致报错。

// server/index.ts
......
const port = parseInt(process.env.PORT ?? '3000');
export default new Server().router(routes).listen(port);

为了快速测试,我只是想证明 Express Server 如何在 Azure 上工作,所以我不想修复 TypeScript 逻辑。而是直接修改了npm run compile生成的编译js文件,tsc生成的文件dist/index.js由Azure执行:

// const port = parseInt((_a = process.env.PORT) !== null && _a !== void 0 ? _a : '3000');
const port = (_a = process.env.PORT) !== null && _a !== void 0 ? _a : '3000';
exports.default = new server_1.default().router(routes_1.default).listen(port);

再次部署,它现在可以工作了:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多