Skip to content

Instantly share code, notes, and snippets.

@rcoedo
Last active August 12, 2022 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcoedo/5e92154c5a646032f550dc14c3105830 to your computer and use it in GitHub Desktop.
Save rcoedo/5e92154c5a646032f550dc14c3105830 to your computer and use it in GitHub Desktop.
const protoLoader = require("@grpc/proto-loader");
const proto = protoLoader.loadSync(path.join(__dirname, "..", "greeter.proto"));
const definition = grpc.loadPackageDefinition(proto);
This gist contains the examples for the article "Node.js gRPC Service in Style".
- https://medium.com/trabe/node-js-grpc-services-in-style-222389be991a
- https://rcoedo.com/blog/2019/01/18/node-js-grpc-services-in-style
syntax= "proto3";
service HelloWorldService {
rpc GreetMe (GreetRequest) returns (GreetReply) {}
}
message GreetRequest {
string name = 1;
}
message GreetReply {
string reply = 1;
}
const grpc = require("@grpc/grpc-js");
const greetMe = (call, callback) => {
callback(null, { reply: `Hey ${call.request.name}!` });
};
const server = new grpc.Server();
server.addService(definition.GreeterService.service, { greetMe });
const path = require("path");
const grpc = require("@grpc/grpc-js");
const protoLoader = require("@grpc/proto-loader");
const proto = protoLoader.loadSync(path.join(__dirname, "..", "greeter.proto"));
const definition = grpc.loadPackageDefinition(proto);
const greetMe = (call, callback) => {
callback(null, { reply: `Hey ${call.request.name}!` });
};
const server = new grpc.Server();
server.addService(definition.GreeterService.service, { greetMe });
server.bindAsync("0.0.0.0:50051", grpc.ServerCredentials.createInsecure(), port => {
server.start();
});
server.bindAsync("0.0.0.0:50051", grpc.ServerCredentials.createInsecure(), port => {
server.start();
});
// Before
const greetMe = (call, callback) => {
callback(null, { reply: `Hey ${call.request.name}!` });
};
// After
const greetMe = async (ctx, next) => {
ctx.res = { reply: `Hey ${ctx.req.name}` };
await next();
};
const path = require("path");
const Mali = require("mali");
const greetMe = async (ctx, next) => {
ctx.res = { reply: `Hey ${ctx.req.name}!` };
await next();
};
const app = new Mali(path.join(__dirname, "..", "greeter.proto"));
app.use({ greetMe });
app.start("0.0.0.0:50051");
const path = require("path");
const Mali = require("mali");
const greetMe = async (ctx, next) => {
ctx.res = { reply: `Hey ${ctx.req.name}!` };
await next();
};
const logRequest = async (ctx, next) => {
const start = new Date();
await next();
const ms = new Date() - start;
console.log(`[${start.toLocaleString()}] Request ${ctx.name} took ${ms}ms`);
};
const app = new Mali(path.join(__dirname, "..", "greeter.proto"));
app.use(logRequest);
app.use({ greetMe });
app.start("0.0.0.0:50051");
async (ctx, next) => {
// Do some stuff, maybe.
// cascade through the rest of the middleware chain
await next();
// Do more stuff, or not. ¯\_(ツ)_/¯
}
async (ctx, next) => {
const start = new Date();
await next();
const ms = new Date() - start;
console.log(`[${start.toLocaleString()}] Request ${ctx.name} took ${ms}ms`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment