feat: add router

This commit is contained in:
2025-03-02 02:18:45 +08:00
parent de3187f5f3
commit bac3e5b393
5 changed files with 65 additions and 2 deletions

3
src/router-simple-lib.ts Normal file
View File

@@ -0,0 +1,3 @@
import { parseXml } from './server/parse-xml.ts';
export { parseXml };

View File

@@ -21,4 +21,4 @@ export const parseBody = async (req: http.IncomingMessage) => {
export const parseSearch = (req: http.IncomingMessage) => {
const parsedUrl = url.parse(req.url, true);
return parsedUrl.query;
}
}

31
src/server/parse-xml.ts Normal file
View File

@@ -0,0 +1,31 @@
import xml2js from 'xml2js';
export const parseXml = async (req: any): Promise<any> => {
return await new Promise((resolve) => {
// 读取请求数据
let data = '';
req.setEncoding('utf8');
// 监听data事件接收数据片段
req.on('data', (chunk) => {
data += chunk;
});
// 当请求结束时处理数据
req.on('end', () => {
try {
// 使用xml2js解析XML
xml2js.parseString(data, function (err, result) {
if (err) {
console.error('XML解析错误:', err);
resolve(null);
} else {
const jsonString = JSON.stringify(result);
resolve(jsonString);
}
});
} catch (error) {
console.error('处理请求时出错:', error);
resolve(null);
}
});
});
};