File tree Expand file tree Collapse file tree 7 files changed +43
-182
lines changed Expand file tree Collapse file tree 7 files changed +43
-182
lines changed Original file line number Diff line number Diff line change 1
1
class NotesHandler {
2
2
constructor ( service ) {
3
3
this . _service = service ;
4
+
5
+ // bind global variable this
6
+ this . postNoteHandler = this . postNoteHandler . bind ( this ) ;
7
+ this . getNotesHandler = this . getNotesHandler . bind ( this ) ;
8
+ this . getNoteByIdHandler = this . getNoteByIdHandler . bind ( this ) ;
9
+ this . putNoteByIdHandler = this . putNoteByIdHandler . bind ( this ) ;
10
+ this . deleteNoteByIdHandler = this . deleteNoteByIdHandler . bind ( this ) ;
4
11
}
5
12
6
13
postNoteHandler ( request , h ) {
Original file line number Diff line number Diff line change
1
+ const NotesHandler = require ( './handler' ) ;
2
+ const routes = require ( './routes' ) ;
3
+
4
+ module . exports = {
5
+ name : 'notes' ,
6
+ version : '1.0.0' ,
7
+ register : async ( server , { service } ) => {
8
+ const notesHandler = new NotesHandler ( service ) ;
9
+ server . route ( routes ( notesHandler ) ) ;
10
+ } ,
11
+ } ;
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
const Hapi = require ( '@hapi/hapi' ) ;
2
- const routes = require ( './routes' ) ;
2
+ const notes = require ( './api/notes' ) ;
3
+ const NotesService = require ( './services/inMemory/NotesService' ) ;
3
4
4
5
const init = async ( ) => {
5
- const server = Hapi . server ( {
6
- port : 5000 ,
7
- host : process . env . NODE_ENV !== 'production' ? 'localhost' : '0.0.0.0' ,
8
- routes : {
9
- cors : {
10
- origin : [ '*' ] ,
11
- } ,
12
- } ,
13
- } ) ;
6
+ const notesService = new NotesService ( ) ;
14
7
15
- server . route ( routes ) ;
8
+ const server = Hapi . server ( {
9
+ port : 5000 ,
10
+ // eslint-disable-next-line no-undef
11
+ host : process . env . NODE_ENV !== 'production' ? 'localhost' : '0.0.0.0' ,
12
+ routes : {
13
+ cors : {
14
+ origin : [ '*' ] ,
15
+ } ,
16
+ } ,
17
+ } ) ;
16
18
17
- await server . start ( ) ;
18
- console . log ( `Server berjalan pada ${ server . info . uri } ` ) ;
19
+ await server . register ( {
20
+ plugin : notes ,
21
+ options : {
22
+ service : notesService ,
23
+ } ,
24
+ } ) ;
25
+
26
+ await server . start ( ) ;
27
+ console . log ( `Server berjalan pada ${ server . info . uri } ` ) ;
19
28
} ;
20
29
21
30
init ( ) ;
Original file line number Diff line number Diff line change
1
+ const { nanoid } = require ( 'nanoid' ) ;
2
+
1
3
class NotesService {
2
4
constructor ( ) {
3
5
this . _notes = [ ] ;
4
6
}
5
7
6
8
addNote ( { title, body, tags} ) {
7
- // eslint-disable-next-line no-undef
8
- const id = nano ( 16 ) ;
9
+ const id = nanoid ( 16 ) ;
9
10
const createdAt = new Date ( ) . toISOString ( ) ;
10
11
const updatedAt = createdAt ;
11
12
You can’t perform that action at this time.
0 commit comments