-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
50 lines (41 loc) · 1.05 KB
/
server.ts
File metadata and controls
50 lines (41 loc) · 1.05 KB
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 * as trpcExpress from '@trpc/server/adapters/express';
import { getPayloadClient } from './services/get-payload';
import { nextApp, nextHandler } from './utils/next-utils';
import { appRouter } from './lib/trpc';
const app = express();
const PORT = Number(process.env.PORT) || 3001;
const createContext = ({
req,
res,
}: trpcExpress.CreateExpressContextOptions) => ({
req,
res,
});
const start = async () => {
const payload = await getPayloadClient({
initOptions: {
express: app,
onInit: async (cms) => {
cms.logger.info(`Admin URL: ${cms.getAdminURL()}`);
},
},
});
app.use(
'/api/trpc',
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
}),
);
app.use((req, res) => nextHandler(req, res));
nextApp.prepare().then(() => {
payload.logger.info('Next.js started');
app.listen(PORT, async () => {
payload.logger.info(
`Next.js App URL: ${process.env.NEXT_PUBLIC_SERVER_URL}`,
);
});
});
};
start();