Skip to content
Blog
Example
Examples
  index: "Intro",
  "-- other": {
    type: "separator",
    title: "Others",
  },
  "db-service": "DB Service",
  example: {
    title: "Examples ↗",
    href: "https://github.com/chakhsu/grpcity/tree/main/example",
    newWindow: true,
  },
  changelog: {
    title: "Changelog ↗",
    href: "https://github.com/chakhsu/grpcity/releases",
    newWindow: true,
  },
}

Import Loader

./server.js
import loader from "./loader.js"

Implement Greeter

Create Greeter and implement the sayGreet method:

./server.js
class Greeter {
  constructor() {
    this.count = 0
  }
 
  async sayGreet(call) {
    const { name } = call.request
    this.count++
    return {
      message: `hello ${name || "world"} by Greeter`,
      count: this.count,
    }
  }
}

Advanced

Please select one of the following guides for a deeper understanding and learning:

  options={[
    ["name", "String", "Required, service name including package name"],
    [
      "implementation",
      "Object",
      "Required, object or class containing methods defined in service rpc",
    ],
    ["options", "Object", "Optional, { exclude, inherit }"],
  ]}
/>

Heading

With bg

⚠️
Warning code looking
enter a
  enter b
    enter c
      rpc-method()
    exit  c
  exit  b
exit  a
Terminal
{
  path: '/helloworld.Greeter/SayGreet',
  method: {
    requestStream: false,
    responseStream: false,
    metadata: Metadata { internalRepr: [Map], options: {} },
    options: { deadline: 2024-01-16T16:22:11.836Z }
  },
  request: { name: 'greeter' }
}
Field NameValid ValuesDescription
keepCasetrue or falsePreserve field names. By default, they are changed to camel case.
longsString or NumberType used to represent long values. Default is the Long object type.
enumsStringType used to represent enum values. Default is a numeric type.
bytesArray or StringType used to represent bytes values. Default is Buffer.
defaultstrue or falseSet default values on the output object. Default is false.
arraystrue or falseSet empty arrays for missing array values, even if defaults is false. Default is false.
objectstrue or falseSet empty objects for missing object values, even if defaults is false. Default is false.
oneofstrue or falseSet virtual oneof properties as field names. Default is false.
jsontrue or falseRepresent Infinity and NaN as strings in float fields and automatically decode google.protobuf.Any values. Default is false.
includeDirsArray of stringsList of search paths for imported .proto files.
Terminal
.
└── certs
    ├── ca.crt
    ├── ca.key
    ├── client.crt
    ├── client.csr
    ├── client.key
    ├── server.crt
    ├── server.csr
    └── server.key
./loader.js
import { ProtoLoader } from "grpcity"
import path from "node:path"
import { fileURLToPath } from "node:url"
 
// __dirname for esm
const __dirname = path.dirname(fileURLToPath(import.meta.url))
 
export default new ProtoLoader({
  location: path.resolve(__dirname, "./"),
  files: ["stream.proto"],
})

© 2024 CompileAndCry