Azure 应用服务 - Complied Typescript 项目不会运行

Azure App Service - Complied Typescript project will not run

提问人:user3067684 提问时间:11/15/2023 更新时间:11/15/2023 访问量:59

问:

我有一个 Node.js / Typescript / Express 服务,它是一个 REST API(带有 Swagger Docs)

我正在尝试通过 Azure 应用服务托管它,但没有成功。

我可以在本地构建和运行,但在 AppService 中没有成功。

理想情况下,我想构建一个并将其导出为独立的 Node.js 服务。但现在我会满足于它的工作。dist

在我的package.json文件中...

"scripts": {
        "build": "npx tsc  --build",
        "start": "node dist/server.js --port 8080",
        "dev": "nodemon ./src/server.ts",
        "lint": "eslint --ignore-path .eslintignore --ext .js,.ts",
        "format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
        "test": "mocha -r ts-node/register 'src/**/*.spec.ts'"
    },

错误/跟踪:

Enter the source directory to make sure the script runs where the user expects
2023-11-15T13:57:37.520261569Z cd "/home/site/wwwroot"
2023-11-15T13:57:37.520265269Z
2023-11-15T13:57:37.520268770Z export NODE_PATH=/usr/local/lib/node_modules:$NODE_PATH
2023-11-15T13:57:37.520272870Z if [ -z "$PORT" ]; then
2023-11-15T13:57:37.520276570Z      export PORT=8080
2023-11-15T13:57:37.520280470Z fi
2023-11-15T13:57:37.520283970Z
2023-11-15T13:57:37.520287570Z npm start
2023-11-15T13:57:38.128405653Z npm info using [email protected]
2023-11-15T13:57:38.128753158Z npm info using [email protected]
2023-11-15T13:57:38.165803018Z
2023-11-15T13:57:38.165824618Z > [email protected] start
2023-11-15T13:57:38.165830518Z > node dist/server.js --port 8080
2023-11-15T13:57:38.165834418Z
2023-11-15T13:57:38.302706285Z node:internal/modules/cjs/loader:1080
2023-11-15T13:57:38.302769386Z   throw err;
2023-11-15T13:57:38.302776286Z   ^
2023-11-15T13:57:38.302791386Z
2023-11-15T13:57:38.302795386Z Error: Cannot find module 'cors'
2023-11-15T13:57:38.302799287Z Require stack:
2023-11-15T13:57:38.302802987Z - /home/site/wwwroot/dist/server.js
2023-11-15T13:57:38.302806987Z     at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
2023-11-15T13:57:38.302810987Z     at Module._load (node:internal/modules/cjs/loader:922:27)
2023-11-15T13:57:38.302815187Z     at Module.require (node:internal/modules/cjs/loader:1143:19)
2023-11-15T13:57:38.302818987Z     at require (node:internal/modules/cjs/helpers:121:18)
2023-11-15T13:57:38.302822787Z     at Object.<anonymous> (/home/site/wwwroot/dist/server.js:16:32)
2023-11-15T13:57:38.302835787Z     at Module._compile (node:internal/modules/cjs/loader:1256:14)
2023-11-15T13:57:38.302839587Z     at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
2023-11-15T13:57:38.302843187Z     at Module.load (node:internal/modules/cjs/loader:1119:32)
2023-11-15T13:57:38.302847087Z     at Module._load (node:internal/modules/cjs/loader:960:12)
2023-11-15T13:57:38.302851187Z     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
2023-11-15T13:57:38.302855387Z   code: 'MODULE_NOT_FOUND',
2023-11-15T13:57:38.302859287Z   requireStack: [ '/home/site/wwwroot/dist/server.js' ]
2023-11-15T13:57:38.302863388Z }
2023-11-15T13:57:38.306441642Z
2023-11-15T13:57:38.306461842Z Node.js v18.17.1
2023-11-15T13:57:36.126Z INFO  - 18-lts_20230908.2.tuxprod Pulling from appsvc/node
2023-11-15T13:57:36.139Z INFO  -  Digest: sha256:f968f5d09a5e36f4e447bb8e3073f284b3ecc4bb60cf72d1243c35dc6fd5b29e
2023-11-15T13:57:36.140Z INFO  -  Status: Image is up to date for mcr.microsoft.com/appsvc/node:18-lts_20230908.2.tuxprod
2023-11-15T13:57:36.153Z INFO  - Pull Image successful, Time taken: 0 Minutes and 0 Seconds
2023-11-15T13:57:36.171Z INFO  - Starting container for site
2023-11-15T13:57:36.172Z INFO  - docker run -d --expose=8080 --name app-strata-open-cms-uksouth-demo_0_28bf6b29 -e WEBSITE_SITE_NAME=app-strata-open-cms-uksouth-demo -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=app-strata-open-cms-uksouth-demo.azurewebsites.net -e WEBSITE_INSTANCE_ID=bd0010bcc7f2a648cafc4bcac18f1660692939e3ec29400aeb60d8a3d788c1c3 -e WEBSITE_USE_DIAGNOSTIC_SERVER=True appsvc/node:18-lts_20230908.2.tuxprod
2023-11-15T13:57:36.172Z INFO  - Logging is not enabled for this container.Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2023-11-15T13:57:36.549Z INFO  - Initiating warmup request to container app-strata-open-cms-uksouth-demo_0_28bf6b29 for site app-strata-open-cms-uksouth-demo
2023-11-15T13:57:39.424Z ERROR - Container app-strata-open-cms-uksouth-demo_0_28bf6b29 for site app-strata-open-cms-uksouth-demo has exited, failing site start
2023-11-15T13:57:39.437Z ERROR - Container app-strata-open-cms-uksouth-demo_0_28bf6b29 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2023-11-15T13:57:39.441Z INFO  - Stopping site app-strata-open-cms-uksouth-demo because it failed during startup.
azure-web-app-service azure-appservice

评论

0赞 Harshitha 11/20/2023
您是如何部署 Web 应用程序的?

答:

0赞 Aslesha Kantamsetti 11/23/2023 #1

我使用 NodeJs/TypeScript/Express 和 Swagger Docs 创建了应用,并使用 Visual Studio Code Azure 扩展作为 Web 应用部署到 Azure 应用服务并成功部署。

我已将完整的项目文件部署到 Azure 应用服务。enter image description here

它已成功部署到 Azure,如下所示:enter image description here

Azure 中的 Web 应用 OS 是 Linux,运行时版本是节点 18enter image description here

确保输入了正确的 swagger 路径。enter image description here

wwwroot floder: enter image description here

这是我的package.json文件

"scripts": {
"start": "node dist/app.js",
"dev": "tsc -w & nodemon dist/app.js"
},

dist/app.js 文件

"use strict";
var  __importDefault  = (this  &&  this.__importDefault) ||  function (mod) {
return (mod  &&  mod.__esModule) ?  mod  : { "default":  mod };
};
Object.defineProperty(exports, "__esModule", { value:  true });
// src/app.ts
const  express_1  =  __importDefault(require("express"));
const  swagger_jsdoc_1  =  __importDefault(require("swagger-jsdoc"));
const  swagger_ui_express_1  =  __importDefault(require("swagger-ui-express"));
const  routes_1  =  __importDefault(require("./routes"));
const  cors_1  =  __importDefault(require("cors"));
const  app  = (0, express_1.default)();
const  port  =  process.env.PORT  ||  3000; 
app.use(express_1.default.json());
// Swagger configuration
const  options  = {
definition: {
openapi:  '3.0.0',
info: {
title:  'My API',
version:  '1.0.0',
},
components: {
schemas: {
User: {
type:  'object',
properties: {
id: { type:  'integer' },
username: { type:  'string' },
email: { type:  'string' },
},
required: ['id', 'username', 'email'],
},
},
},
},
apis: ['./src/controllers/*.ts', './src/routes/*.ts'],
};
const  specs  = (0, swagger_jsdoc_1.default)(options);
app.use('/api-docs', swagger_ui_express_1.default.serve, swagger_ui_express_1.default.setup(specs, { swaggerOptions: { url:  '/api-docs/swagger.json' } }));
// Use the aggregated routes
app.use(routes_1.default);
app.use((0, cors_1.default)());
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});