我正在尝试使用此 Express 生成器为 API 服务创建 Express 服务器:
generator-express-no-stress-typescript
我可以使用 npm run dev
在本地运行 Express Server。
我可以运行 npm runcompile
在 dist 文件夹中创建生产版本。
可以使用 npm run start
毫无问题地启动 dist 文件夹产品包。
但是,我未能将该应用部署到我的 Azure 服务器。我已关注this Microsoft document ,使用 Visual Studio Code 将 Express.js 部署到 Azure 应用服务。完成所有步骤后,当我浏览应用程序时,它显示您无权查看此目录或页面。
有什么文章或文档可以引用吗?感谢您的帮助。
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"
},
最佳答案
我终于弄清楚我的包和部署出了什么问题。在这里我将分享我的发现。
Azure 需要 iisnode/web.config 来执行应用程序
当我回顾this Microsoft tutorial document时再次,我在他们的示例项目中发现,根文件夹中有一个 web.config 文件
引用此链接:https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
它说“如果使用iisnode在后面运行 Node 进程,则需要此配置文件”
我将相同的文件复制到我的项目中,我的项目是由此生成器生成的:generator-express-no-stress-typescript
我对我的项目做了什么:
- 使用生成器生成示例 Express API 服务器
- 运行
npm runcompile
将产品版本编译到dist
文件夹 - 将上述 web.config 复制到根文件夹
- 修改内容,路径指向
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,现在日志文件错误消息发生了变化。现在它显示类似“端口号无效,应在 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 runcompile生成的编译后的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);
再次部署,现在可以运行了:)
关于node.js - 如何将 TypeScript 中的 ExpressJS 部署到 Azure?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67914858/