-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
40 lines (32 loc) · 924 Bytes
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { inferAsyncReturnType, initTRPC } from "@trpc/server";
import * as trpcExpress from "@trpc/server/adapters/express";
import cors from "cors";
import express from "express";
import { z } from "zod";
const createContext = ({
req,
res,
}: trpcExpress.CreateExpressContextOptions) => ({ req, res });
type Context = inferAsyncReturnType<typeof createContext>;
const t = initTRPC.context<Context>().create();
const router = t.router;
const publicProcedure = t.procedure;
export const appRouter = router({
greeting: publicProcedure
.input(z.object({ text: z.string() }))
.mutation(({ input }) => {
return { message: `Hello ${input.text}` };
}),
});
const app = express();
app.use(cors());
app.use(
"/trpc",
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
})
);
app.listen(3001, () => {
console.log(`🚀 Server running on http://localhost:3001`);
});