-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
63 lines (49 loc) · 1.39 KB
/
server.js
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
const grpc = require('grpc');
const uuid = require('uuid/v1');
const todoproto = grpc.load('todo.proto')
const server = new grpc.Server()
let todos = [
{ id : '1',title : 'Todo 1',iscompleted : false }
]
server.addService(todoproto.TodoService.service,{
list : (_,callback) =>{
callback(null,todos)
},
insert : (call,callback) => {
let todo = call.request;
todo.id = uuid()
todos.push(todo)
callback(null,todo)
},
update : (call,callback) => {
let todo = todos.find((t) => t.id === call.request.id);
if(todo){
todo.title = call.request.title
todo.iscompleted = call.request.iscompleted
callback(null,todo)
}
else{
callback({
code : grpc.status.NOT_FOUND,
details : "Not Found"
})
}
},
delete : (call,callback) => {
let todoDelete = todos.find((n) => n.id === call.request.id);
if(todoDelete != -1){
todos.splice(todoDelete,1)
callback(null,{})
}
else{
callback({
code : grpc.status.NOT_FOUND,
details : "Not Found"
})
}
}
})
server.bind('127.0.0.1:50051',
grpc.ServerCredentials.createInsecure())
console.log('server is running at http://127.0.0.1:50051')
server.start()