From 12491ac7c0e00a60212f37a34ff457fcd489584e Mon Sep 17 00:00:00 2001 From: xream Date: Wed, 13 Dec 2023 00:26:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Node.js=20=E5=89=8D=E7=AB=AF=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E5=90=8E=E7=AB=AF=E8=B7=AF=E7=94=B1=20=E9=9C=80?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=20?= =?UTF-8?q?=E6=B3=A8=E6=84=8F=E5=AE=89=E5=85=A8=20`SUB=5FSTORE=5FFRONTEND?= =?UTF-8?q?=5FPATH=3D/prefix`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/restful/index.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/backend/src/restful/index.js b/backend/src/restful/index.js index 7b16b10..4eefe79 100644 --- a/backend/src/restful/index.js +++ b/backend/src/restful/index.js @@ -41,6 +41,7 @@ export default function serve() { if ($.env.isNode) { const path = eval(`require("path")`); const fs = eval(`require("fs")`); + const fe_be_path = eval('process.env.SUB_STORE_FRONTEND_BACKEND_PATH'); const fe_port = eval('process.env.SUB_STORE_FRONTEND_PORT') || 3001; const fe_host = eval('process.env.SUB_STORE_FRONTEND_HOST') || host || '::'; @@ -67,19 +68,38 @@ export default function serve() { const staticFileMiddleware = express_.static(fe_path); - app.use('/api', createProxyMiddleware(`http://127.0.0.1:${port}`)); + if (fe_be_path) { + app.use( + fe_be_path, + createProxyMiddleware({ + target: `http://127.0.0.1:${port}`, + changeOrigin: true, + ws: true, + pathRewrite: { + [`^${fe_be_path}/api/`]: '/api/', + }, + }), + ); + } + app.use(staticFileMiddleware); app.use( history({ disableDotRule: true, - verbose: true, + verbose: false, }), ); app.use(staticFileMiddleware); const listener = app.listen(fe_port, fe_host, () => { - const { address, port } = listener.address(); - $.info(`[FRONTEND] ${address}:${port}`); + const { address: fe_address, port: fe_port } = + listener.address(); + $.info(`[FRONTEND] ${fe_address}:${fe_port}`); + if (fe_be_path) { + $.info( + `[FRONTEND -> BACKEND] ${fe_address}:${fe_port}${fe_be_path}/api/ -> http://127.0.0.1:${port}/api/`, + ); + } }); } }