-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
60 lines (46 loc) · 1.29 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import dotenv from 'dotenv'
import next from 'next'
import nextBuild from 'next/dist/build'
import path from 'path'
dotenv.config({
path: path.resolve(__dirname, '../.env'),
})
import express from 'express'
import payload from 'payload'
// import { seed } from './payload/seed'
const app = express()
const PORT = process.env.PORT || 3000
const start = async (): Promise<void> => {
await payload.init({
secret: process.env.PAYLOAD_SECRET || '',
express: app,
onInit: () => {
payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
},
})
if (process.env.PAYLOAD_SEED === 'true') {
// await seed(payload)
process.exit()
}
if (process.env.NEXT_BUILD) {
app.listen(PORT, async () => {
payload.logger.info(`Next.js is now building...`)
// @ts-expect-error
await nextBuild(path.join(__dirname, '../'))
process.exit()
})
return
}
const nextApp = next({
dev: process.env.NODE_ENV !== 'production',
})
const nextHandler = nextApp.getRequestHandler()
app.use((req, res) => nextHandler(req, res))
nextApp.prepare().then(() => {
payload.logger.info('Starting Next.js...')
app.listen(PORT, async () => {
payload.logger.info(`Next.js App URL: ${process.env.PAYLOAD_PUBLIC_SERVER_URL}`)
})
})
}
start()