콘텐츠로 이동
📣 인포그랩에서 OpenAI 기술 기반으로 자체 개발한 자동화 번역 프로그램을 통해 n8n 공식 문서의 한글판을 국내 최초로 제공합니다.

노드 빌딩 접근 방법 선택#

n8n에는 선언적 스타일과 프로그래밍적 스타일이라는 두 가지 노드 빌딩 스타일이 있습니다.

대부분의 노드에는 선언적 스타일을 사용하는 것이 좋습니다. 이 스타일은:

  • JSON 기반 구문을 사용하여 작성하기 간편하며, 버그를 유발할 위험이 적습니다.
  • 향후 호환성이 더 좋습니다.
  • REST API와의 통합을 지원합니다.

프로그램적 스타일은 더 장황합니다. 다음의 경우에 프로그램적 스타일을 사용해야 합니다:

  • 트리거 노드
  • REST 기반이 아닌 모든 노드. 여기에는 GraphQL API를 호출해야 하는 노드와 외부 종속성을 사용하는 노드가 포함됩니다.
  • 수신 데이터를 변환해야 하는 모든 노드.
  • 전체 버전 관리. 버전 관리 유형에 대한 자세한 내용은 노드 버전 관리를 참조하세요.

데이터 처리 차이점#

선언적 스타일과 프로그래밍적 스타일의 주요 차이점은 수신 데이터를 처리하고 API 요청을 빌드하는 방식입니다. 프로그래밍적 스타일에는 수신 데이터와 매개변수를 읽고 요청을 빌드하는 execute() 메소드가 필요합니다. 선언적 스타일은 operations 객체의 routing 키를 사용하여 이를 처리합니다. 노드 매개변수와 execute() 메소드에 대한 자세한 내용은 노드 기본 파일을 참조하세요.

구문 차이점#

선언적 스타일과 프로그래밍적 스타일의 차이를 이해하기 위해 아래의 두 코드 스니펫을 비교해 보세요. 이 예제는 "FriendGrid"라는 SendGrid 통합의 간소화된 버전을 생성합니다. 다음 코드 스니펫은 완전하지 않으며, 노드 빌딩 스타일의 차이를 강조합니다.

프로그램적 스타일에서:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
	IExecuteFunctions,
	INodeExecutionData,
	INodeType,
	INodeTypeDescription,
	IRequestOptions,
} from 'n8n-workflow';

// FriendGrid 클래스 생성
export class FriendGrid implements INodeType {
  description: INodeTypeDescription = {
    displayName: 'FriendGrid',
    name: 'friendGrid',
    . . .
    properties: [
      {
        displayName: 'Resource',
        . . .
      },
      {
      displayName: 'Operation',
      name: 'operation',
      type: 'options',
      displayOptions: {
        show: {
            resource: [
            'contact',
            ],
        },
      },
      options: [
        {
          name: 'Create',
          value: 'create',
          description: 'Create a contact',
        },
      ],
      default: 'create',
      description: 'The operation to perform.',
    },
    {
      displayName: 'Email',
      name: 'email',
      . . .
    },
    {
      displayName: 'Additional Fields',
      // 선택적 필드 설정
    },
  ],
};

  async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
    let responseData;
    const resource = this.getNodeParameter('resource', 0) as string;
    const operation = this.getNodeParameter('operation', 0) as string;
    // 사용자가 제공한 자격 증명 가져오기
    const credentials = await this.getCredentials('friendGridApi') as IDataObject;

    if (resource === 'contact') {
      if (operation === 'create') {
      // 이메일 입력 받기
      const email = this.getNodeParameter('email', 0) as string;
      // 추가 필드 입력 받기
      const additionalFields = this.getNodeParameter('additionalFields', 0) as IDataObject;
      const data: IDataObject = {
          email,
      };

      Object.assign(data, additionalFields);

      // https://sendgrid.com/docs/api-reference/ 에 정의된 대로 HTTP 요청 만들기
      const options: IRequestOptions = {
        headers: {
            'Accept': 'application/json',
            'Authorization': `Bearer ${credentials.apiKey}`,
        },
        method: 'PUT',
        body: {
            contacts: [
            data,
            ],
        },
        url: `https://api.sendgrid.com/v3/marketing/contacts`,
        json: true,
      };
      responseData = await this.helpers.httpRequest(options);
      }
    }
    // 데이터를 n8n 데이터로 매핑
    return [this.helpers.returnJsonArray(responseData)];
  }
}

선언적 스타일에서:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { INodeType, INodeTypeDescription } from 'n8n-workflow';

// FriendGrid 클래스 생성
export class FriendGrid implements INodeType {
  description: INodeTypeDescription = {
    displayName: 'FriendGrid',
    name: 'friendGrid',
    . . .
    // 기본 요청 구성 설정
    requestDefaults: {
        baseURL: 'https://api.sendgrid.com/v3/marketing'
    },
    properties: [
        {
        displayName: 'Resource',
        . . .
        },
        {
        displayName: 'Operation',
        name: 'operation',
        type: 'options',
        displayOptions: {
          show: {
            resource: [
                'contact',
            ],
          },
        },
        options: [
          {
            name: 'Create',
            value: 'create',
            description: 'Create a contact',
            // 라우팅 객체 추가
            routing: {
                request: {
                method: 'POST',
                url: '=/contacts',
                send: {
                    type: 'body',
                    properties: {
                    email: {{$parameter["email"]}}
                    }
                }
                }
            },
            // 연락처 생성에 대한 응답 처리
            output: {
                postReceive: [
                {
                    type: 'set',
                    properties: {
                    value: '={{ { "success": $response } }}'
                    }
                }
                ]
            }
            },
        ],
        default: 'create',
        description: 'The operation to perform.',
        },
        {
        displayName: 'Email',
        . . .
        },
        {
        displayName: 'Additional Fields',
        // 선택적 필드 설정
        },
    ],
  }
  // execute 메소드 필요 없음
}
인포레터에서 최신 DevOps 트렌드를 격주로 만나보세요!