-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
50 lines (46 loc) · 1.3 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
import express from "express";
import dotenv from "dotenv";
dotenv.config();
import morgan from "morgan";
import * as trpcExpress from "@trpc/server/adapters/express";
import { expressHandler } from "trpc-playground/handlers/express";
import { appRouter } from "./trpc/router";
import { createContext } from "./trpc/trpc";
import prisma from "./config/prisma";
import axios from "axios";
// init express server
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json()); // for parsing application/json
app.use(morgan("dev")); // for pretty logging
// ROUTES
app.get("/", (req, res) => {
res.send("hello, world!");
});
// initialize trpc on express server with playground
const TRPC_ENDPOINT = "/trpc";
const TRPC_PLAYGROUND_ENDPOINT = "/trpc-playground";
app.use(
TRPC_ENDPOINT,
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
})
);
expressHandler({
trpcApiEndpoint: TRPC_ENDPOINT,
playgroundEndpoint: TRPC_PLAYGROUND_ENDPOINT,
router: appRouter,
// uncomment this if you're using superjson
// request: {
// superjson: true,
// },
}).then((handeler: any) => {
app.use(handeler);
});
// start the express server
app.listen(port, () => {
console.log(
`[server]: Server is running at PORT ${port} at ${`http://localhost:${port}`}`
);
});