Я пытаюсь создать функцию Azure с помощью машинописного текста. Я довольно новичок в обоих. Я следовал документации и создал одну функцию Azure, используя шаги, упомянутые в документации Azure.
Документы: [Функция Azure с Typecript](https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-typescript?pivots=nodejs-model-v4)
Когда я создал функцию, используя шаблон Httptrigger, она работала нормально. Я смог получить доступ к httptrigger и получил сообщение Hello World! в ответ. Я использую модель программирования v4, host.json v2, рабочий процесс: узел, версия рабочего времени ~ 4 и версия узла 20.
Моя текущая структура каталогов выглядит следующим образом:
az-createquote-functionapp/
├── .vscode/
├── dist/
├── node_modules/
├── src/
│ ├── functions/
│ │ └── createQuoteHttp.ts
│ ├── index.ts
│ └── telemetry.ts
│ ├── processors/
│ │ └── IRequestProcessorInterface.ts
│ │ └── RequestProcessor.ts
├── .gitignore
├── .funcignore
├── eslint.config.js
├── host.json
├── local-settings.json
├── package-lock.json
├── package.json
└── tsconfig.json
My tsconfig.json:
{
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"moduleResolution": "Node",
"outDir": "dist",
"rootDir": ".",
"sourceMap": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
},
"include": [
"./**/*.ts",
],
"exclude": [
"node_modules",
"dist"
]
}
My package.json
{
"name": "az-createquote-functionapp",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"clean": "rimraf dist",
"prestart": "npm run clean && npm run build",
"start": "func start --verbose",
"lint": "eslint '**/*.{js,mjs,cjs,ts}'",
"lint:fix": "eslint '**/*.{js,mjs,cjs,ts}' --fix",
"test": "echo \"No tests yet...\""
},
"dependencies": {
"@azure/functions": "^4.5.0",
"@azure/functions-opentelemetry-instrumentation": "^0.1.0",
"@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.24",
"@azure/openapi": "^3.0.104",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/auto-instrumentations-node": "^0.49.1",
"@opentelemetry/exporter-trace-otlp-http": "^0.52.1",
"@opentelemetry/sdk-node": "^0.52.1",
"drizzle-orm": "^0.33.0",
"pg": "^8.12.0"
},
"devDependencies": {
"@eslint/js": "^9.8.0",
"@types/node": "^20.14.15",
"@types/pg": "^8.11.6",
"drizzle-kit": "^0.24.0",
"eslint": "^9.8.0",
"globals": "^15.9.0",
"rimraf": "^5.0.0",
"typescript": "^4.9.5",
"typescript-eslint": "^8.0.0"
},
"overrides": {
"eslint": "^9.8.0"
},
"type": "module",
"main": "dist/src/{index.js,functions/*.js}"
}
My function httptrigger.ts
import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
import { RequestProcessor } from '../processors/RequestProcessor';
export async function createQuoteHttp(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`Http function processed request for url "${request.url}"`);
try {
// Use the request processor instance to handle the request
const requestProcessor = new RequestProcessor();
await requestProcessor.process(request, context);
} catch (error : unknown) {
if (error instanceof Error) {
context.log(error.message);
}
}
const name = request.query.get('name') || await request.text() || 'world';
return { body: `Hello, ${name}!` };
};
app.http('createQuoteHttp', {
route: 'V1/createQuote',
methods: ['POST'],
authLevel: 'anonymous',
handler: createQuoteHttp
});
// processors/RequestProcessor.ts
import { HttpRequest, InvocationContext } from '@azure/functions';
import { IRequestProcessorInterface } from './IRequestProcessorInterface';
export class RequestProcessor implements IRequestProcessorInterface {
async process(request: HttpRequest, context: InvocationContext): Promise<void> {
context.log(request.body);
}
}
Как только я попробовал импортировать свой процессор или какой-нибудь другой сервис по сути. Я начинаю получать следующую ошибку:
Error [ERR_MODULE_NOT_FOUND]: Worker was unable to load entry point "dist/src/functions/createQuoteHttp.js": Cannot find module '/xxx/yyy/projects/az-createquote-functionapp/dist/src/processors/RequestProcessor' imported from /xxx/yyy/projects/az-createquote-functionapp/dist/src/functions/createQuoteHttp.js
Однако файлы находятся в правильном месте в папке dist. Триггер с шаблоном по умолчанию работает, но как только я пытаюсь создать какой-либо каталог или другие файлы и импортировать их либо в index.ts, либо в свой триггер.ts. Я начинаю получать эту ошибку. Мне нужно создать множество файлов и каталогов для разделения логики, но я не уверен, почему это не удается.
Я пробовал менять версии узлов, изменять разрешение модуля и т. д. Я новичок как в машинописных текстах, так и в функциях Azure. Я проверил журнал документации и попытался найти его в Google, но у всех ошибка могла быть одинаковой, но проблема всегда была разной.






Вам необходимо иметь приведенные ниже коды в упомянутых файлах.
tsconfig-
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "dist",
"rootDir": ".",
"sourceMap": true,
"strict": false
}
}
пакет.json-
{
"name": "78860727",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"clean": "rimraf dist",
"prestart": "npm run clean && npm run build",
"start": "func start --verbose",
"test": "echo \"No tests yet...\""
},
"dependencies": {
"@azure/functions": "^4.0.0"
},
"devDependencies": {
"@types/node": "^20.x",
"typescript": "^4.0.0",
"rimraf": "^5.0.0"
},
"main": "dist/src/{index.js,functions/*.js}"
}
Процессор запроса-
import { HttpRequest, InvocationContext } from '@azure/functions';
import { IRequestProcessorInterface } from './IRequestProcessorInterface';
export class RequestProcessor implements IRequestProcessorInterface {
async process(request: HttpRequest, context: InvocationContext): Promise<void> {
const name = await request.query.get('name');
context.log('Response is:', name);
}
}
createQuoteHttp-
import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
import { RequestProcessor } from '../processors/RequestProcessor';
export async function createQuoteHttp(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`Http function processed request for url "${request.url}"`);
try {
const requestProcessor = new RequestProcessor();
await requestProcessor.process(request, context);
} catch (error : unknown) {
if (error instanceof Error) {
context.log(error.message);
}
}
const name = await request.text() || await request.json() || 'world';
return { body: `Hello, ${name}!` };
};
app.http('createQuoteHttp', {
route: 'V1/createQuote',
methods: ['POST'],
authLevel: 'anonymous',
handler: createQuoteHttp
});
Я могу получить ожидаемый ответ.
Azure Functions Core Tools
Core Tools Version: 4.0.5907 Commit hash: N/A +807e89766a92b14fd07b9f0bc2bea1d8777ab209 (64-bit)
Function Runtime Version: 4.834.3.22875
[2024-08-13T08:48:56.674Z] Debugger listening on ws://*****/9b7c121***ff0f9
[2024-08-13T08:48:56.676Z] For help, see: https://nodejs.org/en/docs/inspector
[2024-08-13T08:48:56.762Z] Worker process started and initialized.
[2024-08-13T08:48:56.827Z] Debugger attached.
Functions:
createQuoteHttp: [POST] http://localhost:7071/api/V1/createQuote
For detailed output, run func with --verbose flag.
[2024-08-13T08:49:13.411Z] Executing 'Functions.createQuoteHttp' (Reason='This function was programmatically called via the host APIs.', Id=865814c4-1dfa-438d-9396-0f4d9a853e25)
[2024-08-13T08:49:13.546Z] Response is: Afreen
[2024-08-13T08:49:13.546Z] Http function processed request for url "http://localhost:7071/api/V1/createQuote?name=Afreen"
[2024-08-13T08:49:13.575Z] Executed 'Functions.createQuoteHttp' (Succeeded, Id=865814c4-1dfa-438d-9396-0f4d9a853e25, Duration=188ms)

Попробуйте выполнить код в режиме отладки. Перейдите к «Выполнить» -> нажмите «Начать отладку».
Попробуйте с помощью request.headers['user-agent']
Привет, Африн, да, это работает. Большое спасибо. Однако я не знаю, почему всякий раз, когда я пытаюсь получить подробную информацию о заголовках, я продолжаю получать ошибки. Я пробовал request.headers.get('field'), request.headers.entries())[key] и т. д., но продолжаю получать сообщение «Невозможно прочитать свойства undef (чтение 'propetyname')». Не могли бы вы мне тоже помочь с этим?