Fix: server nuxt client directly without proxy

This commit is contained in:
Aram Becker 2023-05-02 10:44:32 +02:00
parent 57994ffb5b
commit a8b7c56bec
7 changed files with 30 additions and 49 deletions

View file

@ -38,7 +38,7 @@ const CronManager = require('./managers/CronManager')
const TaskManager = require('./managers/TaskManager')
class Server {
constructor(SOURCE, PORT, HOST, UID, GID, CONFIG_PATH, METADATA_PATH, ROUTER_BASE_PATH, CLIENT_PORT) {
constructor(SOURCE, PORT, HOST, UID, GID, CONFIG_PATH, METADATA_PATH, ROUTER_BASE_PATH) {
this.Port = PORT
this.Host = HOST
global.Source = SOURCE
@ -48,7 +48,6 @@ class Server {
global.ConfigPath = fileUtils.filePathToPOSIX(Path.normalize(CONFIG_PATH))
global.MetadataPath = fileUtils.filePathToPOSIX(Path.normalize(METADATA_PATH))
global.RouterBasePath = ROUTER_BASE_PATH
global.ClientPort = CLIENT_PORT
global.XAccel = process.env.USE_X_ACCEL
if (!fs.pathExistsSync(global.ConfigPath)) {
@ -219,7 +218,7 @@ class Server {
app.get('/healthcheck', (req, res) => res.sendStatus(200))
// Serve client on all other routes, 404 will be handled by client
router.use(this.clientRouter.router)
app.use(this.clientRouter.router)
this.server.listen(this.Port, this.Host, () => {
if (this.Host) Logger.info(`Listening on http://${this.Host}:${this.Port}`)

View file

@ -68,12 +68,12 @@ class SocketAuthority {
initialize(Server) {
this.Server = Server
this.io = new SocketIO.Server(this.Server.server, {
this.io = (new SocketIO.Server(this.Server.server, {
cors: {
origin: '*',
methods: ["GET", "POST"]
}
})
})).of(global.RouterBasePath || "/")
this.io.on('connection', (socket) => {
this.clients[socket.id] = {
id: socket.id,

View file

@ -1,7 +1,5 @@
const express = require('express')
const Path = require('path')
const childProcess = require('child_process')
const { createProxyMiddleware } = require('http-proxy-middleware');
const Logger = require('../Logger')
class ClientRouter {
@ -15,40 +13,26 @@ class ClientRouter {
this.router.disable('x-powered-by')
}
init() {
const target = `http://localhost:${this.clientPort}${this.routerBasePath || '/'}`
this.router.use(createProxyMiddleware({ target, changeOrigin: true }));
Logger.info(`[Client] Proxying requests to client on port :${this.clientPort}`)
}
start() {
this.init()
async init () {
const clientDir = Path.join(this.appRoot, '/client')
const clientPath = Path.join(clientDir, '/node_modules/@nuxt/cli/bin/nuxt-cli.js')
this.client = childProcess.fork(clientPath, ["start"], { cwd: clientDir, stdio: 'pipe' })
Logger.info(`[Client] Client started under port :${this.clientPort}`)
this.client.stdout.on('data', (data) => {
Logger.info('[Client]', data.toString().trim())
})
this.client.stderr.on('data', (data) => {
Logger.error('[Client]', data.toString().trim())
})
this.client.on('exit', () => {
Logger.info('[Client] Client exited unexpectedly, restarting...')
this.start()
})
const { loadNuxt } = require(Path.resolve(clientDir, 'node_modules/nuxt'))
this.client = await loadNuxt({ rootDir: clientDir, for: 'start' })
}
stop() {
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) {
this.client.off('exit')
this.client.kill()
await this.client.close()
Logger.info('[Client] Stopped')
} else {
Logger.error('Client not running')
}