Skip to content

Commit

Permalink
feat: Node.js 前端代理后端路由
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Dec 12, 2023
1 parent 5e21a20 commit 78e3024
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 13 deletions.
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.14.121",
"version": "2.14.122",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand All @@ -18,6 +18,7 @@
"body-parser": "^1.19.0",
"connect-history-api-fallback": "^2.0.0",
"express": "^4.17.1",
"http-proxy-middleware": "^2.0.6",
"js-base64": "^3.7.2",
"lodash": "^4.17.21",
"request": "^2.88.2",
Expand Down
87 changes: 79 additions & 8 deletions backend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions backend/src/restful/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default function serve() {
let port;
let host;
if ($.env.isNode) {
port = eval('process.env.SUB_STORE_BACKEND_API_PORT');
host = eval('process.env.SUB_STORE_BACKEND_API_HOST');
port = eval('process.env.SUB_STORE_BACKEND_API_PORT') || 3000;
host = eval('process.env.SUB_STORE_BACKEND_API_HOST') || '::';
}
const $app = express({ substore: $, port, host });
// register routes
Expand Down Expand Up @@ -59,11 +59,15 @@ export default function serve() {

const express_ = eval(`require("express")`);
const history = eval(`require("connect-history-api-fallback")`);
const { createProxyMiddleware } = eval(
`require("http-proxy-middleware")`,
);

const app = express_();

const staticFileMiddleware = express_.static(fe_path);

app.use('/api', createProxyMiddleware(`http://127.0.0.1:${port}`));
app.use(staticFileMiddleware);
app.use(
history({
Expand Down
2 changes: 0 additions & 2 deletions backend/src/vendor/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import { ENV } from './open-api';

export default function express({ substore: $, port, host }) {
port = port || 3000;
host = host || '::';
const { isNode } = ENV();
const DEFAULT_HEADERS = {
'Content-Type': 'text/plain;charset=UTF-8',
Expand Down

0 comments on commit 78e3024

Please sign in to comment.