-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (33 loc) · 1.28 KB
/
server.js
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
/* eslint-disable */
const express = require('express')
const { ssr } = require('@sveltech/ssr')
const path = require('path')
const fs = require('fs')
const compression = require('compression')
module.exports.startServers = function (options) {
const { spaPort, ssrPort, serveSpa, serveSsr } = { ...options }
options.host = `http://${options.host}`
options.app = path.resolve(options.distDir, options.app)
options.entrypoint = path.resolve(options.distDir, options.entrypoint)
if (serveSpa) startServer({ ...options, port: spaPort, mode: 'spa' })
if (serveSsr) startServer({ ...options, port: ssrPort, mode: 'ssr' })
}
function startServer(options) {
const { distDir, host, port, mode } = options
const app = express()
const fallback = mode === 'ssr' ? sendSSRRender : sendEntryPoint
// compress all responses
app.use(compression());
app.use(express.static(distDir))
app.get('*', fallback.bind({ options }))
console.log(`Serving ${mode} on ${host}:${port}`)
app.listen(port)
}
async function sendEntryPoint(req, res) {
const { entrypoint } = this.options
res.sendFile(entrypoint)
}
async function sendSSRRender(req, res) {
const { entrypoint, app, host } = this.options
res.send(await ssr(entrypoint, app, req.url, { host }))
}