You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// fungsi handler pada Hapi memiliki 2 parameter yaitu request dan h
3
+
// request parameter merupakan objek yang menampung detail permintaan client, seperti path dan query parameter, payload, header
4
+
// berikut penjelasan lbh lengkap fungsi dari parameter request pada link berikut : https://hapi.dev/api/?v=20.1.0#request-properties
5
+
6
+
// parameter ke-2 yaitu h ( inisial dari hapi)
7
+
// parameter ini merupakan response toolkit yg berfungsi sbg objek yang menampung banyak sekali metod
8
+
// yg digunakan utk menanggapi sebuah permintaan clien.
9
+
// objek ini serupa dg objek response pada request handler ketika menggunakan nodejs native
10
+
11
+
// jika hanya ingin mengembalikan nilai pada sebuah request yang datang, Hapi dapat langsung return nilai dala bentuk teks, html, json, steam atau bahkan promise
12
+
/**
13
+
* server.route({
14
+
method: 'GET',
15
+
path: '/',
16
+
handler: (request, h) => {
17
+
return `Homepage`;
18
+
},
19
+
});
20
+
*/
21
+
22
+
// jika kita dapat mengembalikan request secara singkat, lalu apa fungsi dr h?
23
+
24
+
// pada kasus sederhana diatas, memang lbh baik langsung mengembalikan dengan nilai secara eksplisit.
25
+
// dengan cara tsb, status response selalu bernilai 200 OK.
26
+
// ketika perlu mengubah nilai statust respons, disinilah kita membutuhkan parameter h
27
+
/**
28
+
* server.route({
29
+
method: 'POST',
30
+
path: '/user',
31
+
handler: (request, h) => {
32
+
return h.response('created').code(201);
33
+
},
34
+
});
35
+
*/
36
+
37
+
// fungsi handler harus selalu mengembalikan nilai, bila menggunakan h ketika menangani request,
38
+
// maka kembalikan dg nilai h.response() seperti kode di atas
39
+
40
+
// parameter h tidak hanya berfungsi utk menetapkan status kode respons saja. melali h, anda juga bisa menetapkan header response, content type, content lenght dan banyak lagi
41
+
/**
42
+
* // Detailed notation
43
+
const handler = (request, h) => {
44
+
const response = h.response('success');
45
+
response.type('text/plain');
46
+
response.header('X-Custom', 'some-value');
47
+
return response;
48
+
};
49
+
50
+
// Chained notation
51
+
const handler = (request, h) => {
52
+
return h.response('success')
53
+
.type('text/plain')
54
+
.header('X-Custom', 'some-value');
55
+
};
56
+
*/
57
+
58
+
// untuk mendalami tentang respons toolkit, bisa membaca disini : https://hapi.dev/api/?v=20.1.0#response-toolkit
0 commit comments