-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (32 loc) · 1.09 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 dotenv = require('dotenv')
const next = require('next')
const vhost = require('vhost')
const Vhost = require('./vhost')
const {promises: {readdir, access}} = require("fs");
dotenv.config()
const { env: {PORT, NODE_ENV, DOMAIN_NAME }} = process
const port = PORT || 3000
const domainName = DOMAIN_NAME
const dev = NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const getListSubdomain = async () => {
let pages = await readdir('./pages', {withFileTypes: true})
pages = pages.filter(page=> page.isDirectory)
return pages.map(page => page.name)
}
app.prepare().then(async() => {
const mainServer = express()
const subDomains = await getListSubdomain()
const vHosts = {}
for( subDomain of subDomains) {
vHosts[subDomain] = new Vhost(subDomain, app)
vHosts[subDomain] = vHosts[subDomain].build()
mainServer.use(vhost(`${subDomain}.${domainName}`, vHosts[subDomain]))
}
mainServer.listen(port, (err) => {
if (err) throw err
console.log(`> Ready on ${domainName}:${port}`)
})
})