优化,添加日志

This commit is contained in:
黄心宇 2024-04-23 14:31:31 +08:00
parent 4ea5f61ddf
commit 81c1fa95ae
4 changed files with 26 additions and 10 deletions

View File

@ -4,28 +4,48 @@ import { getDomObj } from "./window"
import { render } from "./render";
import { url, zoneUrl } from "../src/ssrUrl";
const { createProxyMiddleware } = require('http-proxy-middleware');
import Logger from "./log";
const app = express();
app.use('/build', express.static('build'));
// 处理其他静态文件
app.use(express.static(path.resolve(__dirname, '/home/pdl/forgeplus/public')));
const options = (target) => {
return {
target: target,
changeOrigin: false, // 允许在请求头中更改主机
timeout: 30000,
onProxyRes: (proxyRes, req, res) => {
// 代理成功后输出日志
Logger.info(`Proxy to ${req.url} responded with status ${proxyRes.statusCode}`);
},
onError: (err, req, res) => {
// 代理过程中出错时输出日志
Logger.error(`Error proxying ${req.url}:`, err.message);
res.status(500).send('Proxy error');
},
};
}
const proxy = createProxyMiddleware(options('http://localhost:8081'));
// const proxy = createProxyMiddleware(options(url));
// 设置代理
// 本地调试用设置代理
// app.use(['/api/zone', '/api/cms'], (req, res) => {
// const proxy = createProxyMiddleware(options(zoneUrl));
// proxy(req, res);
// });
// 本地调试用设置代理
// app.use(['/api', '/images', '/system', '/favicon', '/robots.txt', '/sitemap*.xml', '/sitemap*.txt', '/favicon.ico', '/admins', '/assets', '/attachments', '/oauth'], (req, res) => {
// proxy(req, res);
// });
// 设置代理
app.use(['/api', '/images', '/system', '/favicon', '/robots.txt', '/sitemap*.xml', '/sitemap*.txt', '/favicon.ico', '/admins', '/assets', '/attachments', '/oauth'], (req, res) => {
app.use(['/api', '/admins', '/attachments', '/oauth'], (req, res) => {
proxy(req, res);
});
@ -43,10 +63,6 @@ function excludePath(req, res, next) {
// 应用中间件到所有路由
app.use(excludePath);
// 处理其他静态文件
app.use(express.static(path.resolve(__dirname, '/home/pdl/forgeplus/public')));
// 匹配路径匹配到的返回处理后的html没匹配到转发到后端
app.get('*',async function (req,res) {
global.domObj = getDomObj()

View File

@ -9,7 +9,7 @@ import Routes, { deepRoutes, specialRoute } from "../Routes";
// import App from "../src/App";
// import { Route } from 'react-router-dom';
// import { setDefaultMeta } from '../src/common/UrlTool'
// import Logger from "./log";
import Logger from "./log";
import { changeHead } from './setHead'
export const render = async (req,res)=>{
@ -51,7 +51,7 @@ export const render = async (req,res)=>{
};
if (_route && !specialRoute.includes(_match.url && _match.url.replace('/', ''))) {
console.log('matchRoot:', _match.url)
Logger.info(`matchRoot: ${ _match.url }`)
await changeHead(_route, _match.params)
} else {
return false

View File

@ -33,7 +33,7 @@ export const changeHead = async ({ key }, params) => {
res = await getOwnerInfo(params.owner);
data = res.data
const name = data.nickname || data.username
if (data) {
if (data && data.status !== 404) {
setSeoMeta(`${name}`, name, data.description || '', `/${params.owner}`)
}
break;

View File

@ -5,7 +5,7 @@ let baseUrl = `${url}/api`
let baseZoneUrl = `${zoneUrl}/api`
if (__CLIENT__) {
let settings = localStorage.chromesetting&&JSON.parse(localStorage.chromesetting);
let settings = localStorage.chromesetting && localStorage.chromesetting !== 'undefined' &&JSON.parse(localStorage.chromesetting);
baseUrl = settings && settings.common.main_site_url + '/api';
baseZoneUrl = settings && settings.common.zone + '/api';
}