Skip to content

Commit ae56aee

Browse files
committed
complete express and pug templating for simple grpc usage
1 parent 5e7f806 commit ae56aee

File tree

7 files changed

+98
-26
lines changed

7 files changed

+98
-26
lines changed

app.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const express = require("express");
2+
3+
// IMPORTING THE CLIENT FOR USING THE FUNCTIONS WE DECLARED IN PROTO FILE
4+
const client = require("./client");
5+
6+
const path = require("path");
7+
8+
const app = express();
9+
10+
app.set("views", path.join(__dirname, "views"));
11+
app.set("view engine", "pug");
12+
13+
app.use(express.json());
14+
app.use(
15+
express.urlencoded({
16+
extended: false,
17+
})
18+
);
19+
20+
app.get("/", (req, res) => {
21+
res.render("home");
22+
});
23+
24+
app.get("/:id", (req, res) => {
25+
/*
26+
the getDetails that we sent from the server to let the client use it is used here
27+
this getDetails takes a id as parameter as we defined in the proto file then gives out a response needed.
28+
*/
29+
client.getDetails({ id: req.params.id }, (err, response) => {
30+
res.render("details", { details: response.message });
31+
});
32+
});
33+
34+
const PORT = process.env.PORT || 3000;
35+
app.listen(PORT, () => {
36+
console.log("Running on port " + PORT);
37+
});

client.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
/* ----------------------------------------------------------------------
2+
This client handles the communication to the express from the grpc server
3+
-------------------------------------------------------------------------*/
4+
15
const PROTO_PATH = __dirname + "/protos/employee.proto";
26

37
const grpc = require("grpc");
48
const protoLoader = require("@grpc/proto-loader");
59
const _ = require("lodash");
610

11+
// PackageDefinition used for generating code from the proto file using protocal buffer compiler
712
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
813
keepCase: true,
914
enums: String,
@@ -14,24 +19,12 @@ const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
1419

1520
const employee_proto = grpc.loadPackageDefinition(packageDefinition).employee;
1621

17-
function main() {
18-
let client = new employee_proto.Employee(
19-
"localhost:3000",
20-
grpc.credentials.createInsecure()
21-
);
22-
23-
if (process.argv.length >= 3) {
24-
employeeid = process.argv[2];
25-
} else {
26-
employeeid = 2;
27-
}
28-
29-
client.getDetails({ id: employeeid }, function (err, response) {
30-
console.log(
31-
`Employee Details from Employee ${employeeid} is: `,
32-
response.message
33-
);
34-
});
35-
}
22+
// Creating client and binding to the port of the grpc server for communcation
23+
// this client holds every function inside employee package
24+
let client = new employee_proto.Employee(
25+
"localhost:3001",
26+
grpc.credentials.createInsecure()
27+
);
3628

37-
main();
29+
// exporting for using on api
30+
module.exports = client;

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"name": "own-tut",
2+
"name": "nodejs-grpc-tutorial",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
9-
"author": "",
10-
"license": "ISC",
9+
"author": "Dovakiin0",
10+
"license": "MIT",
1111
"dependencies": {
1212
"@grpc/proto-loader": "^0.5.5",
1313
"express": "^4.17.1",

server.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
/*-----------------------------------------------------------------------
2+
THIS SERVER IS USED FOR DEFINING LOGIC TO THE FUNCTIONS INSIDE PROTO FILE
3+
AND DECIDES WHAT FUNCTION DOES CLIENT GETS TO USE
4+
-------------------------------------------------------------------------*/
5+
6+
// Importing the proto file needed to use for this service
17
const PROTO_PATH = __dirname + "/protos/employee.proto";
28

9+
// Require packages
310
const grpc = require("grpc");
411
const protoLoader = require("@grpc/proto-loader");
512
const _ = require("lodash");
613

14+
// PackageDefinition used for generating code from the proto file using protocal buffer compiler
715
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
816
keepCase: true,
917
longs: String,
@@ -12,21 +20,30 @@ const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
1220
oneofs: true,
1321
});
1422

23+
// defining the employee proto from the proto file
24+
// the "package employee;" we used in proto file comes here and loads all the service and messages inside
1525
let employeeProto = grpc.loadPackageDefinition(packageDefinition).employee;
1626

27+
// dunmmy data for interaction
1728
const { employees } = require("./dummyEmp");
1829

30+
// the function getDetails we defined in employee proto
1931
function getDetails(call, callback) {
2032
callback(null, {
2133
message: _.find(employees, { id: call.request.id }),
2234
});
2335
}
2436

37+
// running the actual server
2538
function main() {
26-
const server = new grpc.Server();
39+
const server = new grpc.Server(); // instantiate a grpc server
40+
41+
// Adding the service to server and declaring the user-defined function to the proto function
42+
// getDetails is given to let the client know that it can use this function
2743
server.addService(employeeProto.Employee.service, { getDetails: getDetails });
28-
server.bind("0.0.0.0:3000", grpc.ServerCredentials.createInsecure());
29-
console.log("RUNNING ON 3000");
44+
45+
server.bind("0.0.0.0:3001", grpc.ServerCredentials.createInsecure()); // Binds to a port -- createInsecure() is given to say there is no authentication in the server
46+
console.log("RUNNING ON 3001");
3047
server.start();
3148
}
3249

views/details.pug

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
html(lang="en")
3+
head
4+
meta(charset="UTF-8")
5+
meta(name="viewport", content="width=device-width, initial-scale=1.0")
6+
title Details
7+
body
8+
h3 email:
9+
email= details.email
10+
h3 first Name:
11+
fname= details.firstName
12+
h3 last Name:
13+
lname= details.lastName

views/home.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.search-bar {
2+
padding-left: 50;
3+
}

views/home.pug

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
html(lang="en")
3+
head
4+
meta(charset="UTF-8")
5+
meta(name="viewport", content="width=device-width, initial-scale=1.0")
6+
title Document
7+
body
8+
h1 hello
9+

0 commit comments

Comments
 (0)