-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
71 lines (63 loc) · 1.59 KB
/
client.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
63
64
65
66
67
68
69
70
71
const grpc = require('@grpc/grpc-js');
const PROTO_SERVICE_DESCRIPTOR_PATH = './protos/news.proto';
const protoLoader = require('@grpc/proto-loader');
const options = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};
var packageDefinition = protoLoader.loadSync(PROTO_SERVICE_DESCRIPTOR_PATH, options);
const NewsService = grpc.loadPackageDefinition(packageDefinition).NewsService;
const client = new NewsService(
"localhost:50051",
grpc.credentials.createInsecure()
);
client.getAllNews({}, (error, news) => {
if (error) throw error;
console.log("GetAllNews", news);
});
// add a news
client.addNews(
{
title: "Title news 3",
body: "Body content 3",
postImage: "Image URL here",
},
(error, news) => {
if (error) throw error;
console.log("Successfully created a news.", news);
}
);
// edit a news
client.editNews(
{
id: "2",
body: "Body content 2 edited.",
postImage: "Image URL edited.",
title: "Title for 2 edited.",
},
(error, news) => {
if (error) throw error;
console.log("Successfully edited a news.", news);
}
);
// delete a news
client.deleteNews(
{
id: 1,
},
(error, news) => {
if (error) throw error;
console.log("Successfully deleted a news item.", news);
}
);
client.getAllNews({}, (error, news) => {
if (error) throw error;
console.log("GetAllNews", news);
});
client.getNews({ id: 63 }, (error, news) => {
if (error) throw error;
console.log("GetNews", news);
});