-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (33 loc) · 1.05 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
42
43
44
const express = require('express')
const path = require('path')
class Server {
constructor (Prerenderer) {
this._prerenderer = Prerenderer
this._options = {}
this._expressServer = express()
this._nativeServer = null
}
initialize () {
const server = this._expressServer
this._options = this._prerenderer.getOptions()
this._prerenderer.modifyServer(this, 'pre-static')
server.get('*.*', express.static(path.resolve(this._options.outDir), {
dotfiles: 'allow'
}))
this._prerenderer.modifyServer(this, 'post-static')
this._prerenderer.modifyServer(this, 'pre-fallback')
server.get('*', (req, res) => {
res.sendFile( path.join(path.resolve(this._options.outDir), this._options.html || 'index.html'))
})
this._prerenderer.modifyServer(this, 'post-fallback')
return new Promise((resolve, reject) => {
this._nativeServer = server.listen((this._options.port || 1234), () => {
resolve()
})
})
}
close () {
this._nativeServer.close()
}
}
module.exports = Server