The Prim+RPC server can be configured with Fastify. First, create some module to be shared with server
export function hello() {
return "Hi from Prim+RPC!"
}
hello.rpc = "idempotent"
export default hello
Below is a simple example of how to use this module with Fastify:
import { createPrimServer } from "@doseofted/prim-rpc"
import { createMethodHandler } from "@doseofted/prim-rpc-plugins/fastify"
import Fastify from "fastify"
import * as module from "./functions"
const fastify = Fastify()
const methodHandler = createMethodHandler({ fastify })
createPrimServer({ module, methodHandler })
fastify.listen({ port: 3000 })
This configuration does not yet support Files and Blobs but we can do so by adding new Fastify plugins. First, install
both @fastify/multipart
and form-data
using your chosen package manager. Then you can configure it like
so:
import { createPrimServer } from "@doseofted/prim-rpc"
import { createMethodHandler } from "@doseofted/prim-rpc-plugins/fastify"
import Fastify from "fastify"
import multipartPlugin from "@fastify/multipart"
import formDataHandler from "form-data"
const fastify = Fastify()
const methodHandler = createMethodHandler({ fastify, multipartPlugin, formDataHandler })
createPrimServer({ module, methodHandler })
fastify.listen({ port: 3000 })
Now we can test this out with a simple call from the command line:
curl "http://localhost:3000/prim"
You may also choose a compatible method plugin:
Report an Issue