feat: add router
This commit is contained in:
parent
de3187f5f3
commit
bac3e5b393
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://json.schemastore.org/package",
|
"$schema": "https://json.schemastore.org/package",
|
||||||
"name": "@kevisual/router",
|
"name": "@kevisual/router",
|
||||||
"version": "0.0.7",
|
"version": "0.0.8-alpha.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "dist/index.js",
|
"module": "dist/index.js",
|
||||||
@ -26,6 +26,7 @@
|
|||||||
"@types/lodash-es": "^4.17.12",
|
"@types/lodash-es": "^4.17.12",
|
||||||
"@types/node": "^22.13.4",
|
"@types/node": "^22.13.4",
|
||||||
"@types/ws": "^8.5.14",
|
"@types/ws": "^8.5.14",
|
||||||
|
"@types/xml2js": "^0.4.14",
|
||||||
"cookie": "^1.0.2",
|
"cookie": "^1.0.2",
|
||||||
"lodash-es": "^4.17.21",
|
"lodash-es": "^4.17.21",
|
||||||
"nanoid": "^5.1.0",
|
"nanoid": "^5.1.0",
|
||||||
@ -35,6 +36,7 @@
|
|||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"tslib": "^2.8.1",
|
"tslib": "^2.8.1",
|
||||||
"typescript": "^5.7.3",
|
"typescript": "^5.7.3",
|
||||||
|
"xml2js": "^0.6.2",
|
||||||
"zod": "^3.24.2"
|
"zod": "^3.24.2"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -65,6 +67,10 @@
|
|||||||
"./simple": {
|
"./simple": {
|
||||||
"import": "./dist/router-simple.js",
|
"import": "./dist/router-simple.js",
|
||||||
"require": "./dist/router-simple.js"
|
"require": "./dist/router-simple.js"
|
||||||
|
},
|
||||||
|
"./simple-lib": {
|
||||||
|
"import": "./dist/router-simple-lib.js",
|
||||||
|
"require": "./dist/router-simple-lib.js"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -95,4 +95,27 @@ export default [
|
|||||||
},
|
},
|
||||||
plugins: [dts()],
|
plugins: [dts()],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: 'src/router-simple-lib.ts',
|
||||||
|
output: {
|
||||||
|
file: 'dist/router-simple-lib.js',
|
||||||
|
format: 'es',
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
resolve({
|
||||||
|
browser: false,
|
||||||
|
}),
|
||||||
|
commonjs(),
|
||||||
|
typescript(),
|
||||||
|
],
|
||||||
|
external: ['xml2js'],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: 'src/router-simple-lib.ts',
|
||||||
|
output: {
|
||||||
|
file: 'dist/router-simple-lib.d.ts',
|
||||||
|
format: 'es',
|
||||||
|
},
|
||||||
|
plugins: [dts()],
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
3
src/router-simple-lib.ts
Normal file
3
src/router-simple-lib.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { parseXml } from './server/parse-xml.ts';
|
||||||
|
|
||||||
|
export { parseXml };
|
@ -21,4 +21,4 @@ export const parseBody = async (req: http.IncomingMessage) => {
|
|||||||
export const parseSearch = (req: http.IncomingMessage) => {
|
export const parseSearch = (req: http.IncomingMessage) => {
|
||||||
const parsedUrl = url.parse(req.url, true);
|
const parsedUrl = url.parse(req.url, true);
|
||||||
return parsedUrl.query;
|
return parsedUrl.query;
|
||||||
}
|
}
|
||||||
|
31
src/server/parse-xml.ts
Normal file
31
src/server/parse-xml.ts
Normal 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user