Last active
August 12, 2022 09:16
-
-
Save rcoedo/5e92154c5a646032f550dc14c3105830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const protoLoader = require("@grpc/proto-loader"); | |
const proto = protoLoader.loadSync(path.join(__dirname, "..", "greeter.proto")); | |
const definition = grpc.loadPackageDefinition(proto); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax= "proto3"; | |
service HelloWorldService { | |
rpc GreetMe (GreetRequest) returns (GreetReply) {} | |
} | |
message GreetRequest { | |
string name = 1; | |
} | |
message GreetReply { | |
string reply = 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server.bindAsync("0.0.0.0:50051", grpc.ServerCredentials.createInsecure(), port => { | |
server.start(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async (ctx, next) => { | |
// Do some stuff, maybe. | |
// cascade through the rest of the middleware chain | |
await next(); | |
// Do more stuff, or not. ¯\_(ツ)_/¯ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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