audiobookshelf/server/routers/ClientRouter.js
2023-05-02 10:44:32 +02:00

42 lines
1.1 KiB
JavaScript

const express = require('express')
const Path = require('path')
const Logger = require('../Logger')
class ClientRouter {
constructor(appRoot, clientPort = 3000, routerBasePath = '') {
this.appRoot = appRoot
this.clientPort = clientPort
this.routerBasePath = routerBasePath
this.client = null
this.router = express()
this.router.disable('x-powered-by')
}
async init () {
const clientDir = Path.join(this.appRoot, '/client')
const { loadNuxt } = require(Path.resolve(clientDir, 'node_modules/nuxt'))
this.client = await loadNuxt({ rootDir: clientDir, for: 'start' })
}
async start () {
Logger.info('[Client] Starting')
await this.init()
this.router.use(this.client.render)
this.client.hook('error', (err) => Logger.error('[Client]', err))
this.client.ready().then(() => Logger.info('[Client] Ready'))
}
async stop () {
if (this.client) {
await this.client.close()
Logger.info('[Client] Stopped')
} else {
Logger.error('Client not running')
}
}
}
module.exports = ClientRouter