Last active
September 11, 2020 01:34
-
-
Save leleofg/6d6d01c448c46a674d96f21391d384b1 to your computer and use it in GitHub Desktop.
A server grpc with node.js
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"); | |
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