Skip to content

Instantly share code, notes, and snippets.

@leleofg
Last active September 11, 2020 01:34
Show Gist options
  • Save leleofg/6d6d01c448c46a674d96f21391d384b1 to your computer and use it in GitHub Desktop.
Save leleofg/6d6d01c448c46a674d96f21391d384b1 to your computer and use it in GitHub Desktop.
A server grpc with node.js
const grpc = require("grpc");
const protoLoader = require("@grpc/proto-loader");
const PROTO_PATH = __dirname + "/proto/user.proto";
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
defaults: true,
oneofs: true
});
const protos = grpc.loadPackageDefinition(packageDefinition);
function createUser(call, callback) {
// Não vou utilizar essas variáveis, mas a ideia aqui é mostrar como você pode usar
const { email, username, password } = call.request;
callback(null, {
id: 1,
email: "[email protected]",
username: "leo",
password: "123456",
});
}
function getUserById(call, callback) {
// Não vou utilizar a variável "id", mas a ideia aqui é mostrar como você pode usar
const { id } = call.request;
callback(null, {
id: 1,
email: "[email protected]",
username: "leo",
password: "123456",
});
}
function main() {
const server = new grpc.Server();
server.addService(protos.UserService.service, {
createUser,
getUserById,
});
server.bind("0.0.0.0:50051", grpc.ServerCredentials.createInsecure());
server.start();
console.log("server is running");
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment