You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.5 KiB
TypeScript
125 lines
3.5 KiB
TypeScript
2 years ago
|
import path from 'path'
|
||
|
import fs from 'fs/promises'
|
||
|
|
||
|
import express from 'express'
|
||
2 years ago
|
import morgan from 'morgan'
|
||
|
|
||
|
import { createServer as createViteServer } from 'vite'
|
||
|
|
||
|
import { createApiRouter } from './server/routes.js'
|
||
2 years ago
|
import { RenderFunction } from './shared/ssr.js'
|
||
2 years ago
|
|
||
2 years ago
|
const HTML_ROUTES = ['/', '/login', '/problem/:id', '/admin', '/profile']
|
||
2 years ago
|
|
||
2 years ago
|
const config = {
|
||
|
isDevelopment: process.env.MODE === 'development',
|
||
|
port: process.env.PORT || 3000,
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
if (config.isDevelopment) {
|
||
|
console.log(`[Config] Running in development mode`)
|
||
|
console.log(`[Config] PORT = ${config.port}`)
|
||
|
}
|
||
|
|
||
|
function mountIndexHtmlRoutes(r, serveIndexHtml) {
|
||
|
for (const route of HTML_ROUTES) {
|
||
|
console.log(`[Server] Mounted index html for "${route}"`)
|
||
|
r.get(route, serveIndexHtml)
|
||
|
}
|
||
|
|
||
|
r.use('*', (_req, res) => {
|
||
|
res.redirect('/')
|
||
|
})
|
||
|
}
|
||
2 years ago
|
|
||
|
async function createDevRouter() {
|
||
|
const r = express.Router()
|
||
|
|
||
2 years ago
|
const vite = await createViteServer({
|
||
|
server: { middlewareMode: true },
|
||
|
appType: 'custom',
|
||
|
})
|
||
|
|
||
2 years ago
|
r.use(vite.middlewares)
|
||
2 years ago
|
mountIndexHtmlRoutes(r, async (req, res, next) => {
|
||
2 years ago
|
try {
|
||
|
const indexHtml = await fs.readFile(path.resolve('./index.html'), 'utf-8')
|
||
|
const transformedTemplate = await vite.transformIndexHtml(req.originalUrl, indexHtml)
|
||
|
|
||
|
// Load (to be bundled) entry point for server side rendering
|
||
2 years ago
|
const render: RenderFunction = (await vite.ssrLoadModule('./client/entry-server.tsx'))
|
||
|
.default
|
||
2 years ago
|
|
||
2 years ago
|
const { html, metadata } = render(req.originalUrl)
|
||
|
|
||
|
process.stdout.write('[Metadata] ')
|
||
|
console.dir(metadata)
|
||
|
|
||
|
const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`
|
||
|
const metaTagsHtml =
|
||
|
'' +
|
||
|
(metadata.title
|
||
|
? `<meta property="og:title" content="${metadata.title}" />\n`
|
||
|
: '') +
|
||
|
(metadata.description
|
||
|
? `<meta property="og:description" content="${metadata.description}" />\n`
|
||
|
: '') +
|
||
|
`<meta property="og:url" content="${fullUrl}" />\n`
|
||
|
|
||
|
res.send(
|
||
|
transformedTemplate
|
||
|
.replace('<!-- INJECT META TAGS -->', metaTagsHtml)
|
||
|
.replace('<!-- SSR OUTLET -->', html)
|
||
|
)
|
||
2 years ago
|
} catch (error) {
|
||
|
vite.ssrFixStacktrace(error)
|
||
|
next(error)
|
||
|
}
|
||
2 years ago
|
})
|
||
2 years ago
|
|
||
|
return r
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
async function createProductionRouter() {
|
||
|
// Load bundled entry point for server side rendering
|
||
2 years ago
|
const render: RenderFunction = ((await import('./dist/entry-server/entry-server.js')) as any)
|
||
|
.default
|
||
2 years ago
|
|
||
2 years ago
|
const r = express.Router()
|
||
2 years ago
|
|
||
|
r.use('/', express.static('dist/client'))
|
||
2 years ago
|
|
||
|
mountIndexHtmlRoutes(r, async (req, res) => {
|
||
2 years ago
|
const transformedTemplate = await fs.readFile(
|
||
|
path.resolve('./dist/client/index.html'),
|
||
|
'utf-8'
|
||
|
)
|
||
|
|
||
2 years ago
|
const { html, metadata } = render(req.originalUrl)
|
||
2 years ago
|
|
||
2 years ago
|
res.send(transformedTemplate.replace('<!-- SSR OUTLET -->', html))
|
||
2 years ago
|
})
|
||
|
|
||
|
return r
|
||
|
}
|
||
|
|
||
|
async function main() {
|
||
|
const app = express()
|
||
|
|
||
2 years ago
|
app.use(morgan('dev'))
|
||
|
|
||
2 years ago
|
app.use('/', await createApiRouter())
|
||
|
|
||
|
if (config.isDevelopment) {
|
||
|
app.use('/', await createDevRouter())
|
||
|
} else {
|
||
|
app.use('/', await createProductionRouter())
|
||
|
}
|
||
|
|
||
|
app.listen(config.port, () => {
|
||
|
console.log(`Listening on port ${config.port}...`)
|
||
|
})
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
main()
|