Skip to main content

Starting a server

import { ProtoCat } from 'protocat'
import { CatRegisterService } from '../cat_grpc_pb'

const findCat = (name: string) =>
Promise.resolve({
name,
level: Math.round(Math.random() * 10),
health: Math.random() * 100,
})

const app = new ProtoCat()

app.addService(CatRegisterService, {
getCat: async call => {
const cat = await findCat(call.request.getName())
call.response.setName(cat.name).setLevel(cat.level).setHealth(cat.health)
},
})

app.start('0.0.0.0:3000')

Test your running server with GUI client BloomRPC or CLI tool grpcurl:

grpcurl \
-proto cat.proto \
-plaintext \
-d '{"name": "ProtoCat"}' \
localhost:3000 cats.v1.CatRegister.GetCat

And you should get a response! 🎉

{
"name": "ProtoCat",
"health": 79.40545,
"level": "1"
}