router writeHead修改
This commit is contained in:
parent
8823c15aba
commit
06c3cc4236
20
package.json
20
package.json
@ -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.9",
|
"version": "0.0.10-beta.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "dist/index.js",
|
"module": "dist/index.js",
|
||||||
@ -20,22 +20,22 @@
|
|||||||
"author": "abearxiong",
|
"author": "abearxiong",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-commonjs": "^28.0.2",
|
"@rollup/plugin-commonjs": "^28.0.3",
|
||||||
"@rollup/plugin-node-resolve": "^16.0.0",
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
||||||
"@rollup/plugin-typescript": "^12.1.2",
|
"@rollup/plugin-typescript": "^12.1.2",
|
||||||
"@types/lodash-es": "^4.17.12",
|
"@types/lodash-es": "^4.17.12",
|
||||||
"@types/node": "^22.13.4",
|
"@types/node": "^22.13.11",
|
||||||
"@types/ws": "^8.5.14",
|
"@types/ws": "^8.18.0",
|
||||||
"@types/xml2js": "^0.4.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.5",
|
||||||
"rollup": "^4.34.8",
|
"rollup": "^4.36.0",
|
||||||
"rollup-plugin-dts": "^6.1.1",
|
"rollup-plugin-dts": "^6.2.1",
|
||||||
"ts-loader": "^9.5.2",
|
"ts-loader": "^9.5.2",
|
||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"tslib": "^2.8.1",
|
"tslib": "^2.8.1",
|
||||||
"typescript": "^5.7.3",
|
"typescript": "^5.8.2",
|
||||||
"xml2js": "^0.6.2",
|
"xml2js": "^0.6.2",
|
||||||
"zod": "^3.24.2"
|
"zod": "^3.24.2"
|
||||||
},
|
},
|
||||||
@ -46,7 +46,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"path-to-regexp": "^8.2.0",
|
"path-to-regexp": "^8.2.0",
|
||||||
"selfsigned": "^2.4.1",
|
"selfsigned": "^2.4.1",
|
||||||
"ws": "^8.18.0"
|
"ws": "^8.18.1"
|
||||||
},
|
},
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"access": "public"
|
"access": "public"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { pathToRegexp, match, Key } from 'path-to-regexp';
|
import { pathToRegexp, Key } from 'path-to-regexp';
|
||||||
import type { IncomingMessage, ServerResponse } from 'http';
|
import type { IncomingMessage, ServerResponse } from 'http';
|
||||||
import { parseBody, parseSearch } from './server/parse-body.ts';
|
import { parseBody, parseSearch } from './server/parse-body.ts';
|
||||||
|
|
||||||
@ -14,12 +14,12 @@ interface Route {
|
|||||||
*/
|
*/
|
||||||
export class SimpleRouter {
|
export class SimpleRouter {
|
||||||
routes: Route[] = [];
|
routes: Route[] = [];
|
||||||
|
exclude: string[] = []; // 排除的请求
|
||||||
constructor() {
|
constructor(opts?: { exclude?: string[] }) {
|
||||||
// console.log('AppSimple initialized');
|
this.exclude = opts?.exclude || ['/api/router'];
|
||||||
}
|
}
|
||||||
getBody(req: Req) {
|
getBody(req: Req) {
|
||||||
return parseBody(req);
|
return parseBody<Record<string, any>>(req);
|
||||||
}
|
}
|
||||||
getSearch(req: Req) {
|
getSearch(req: Req) {
|
||||||
return parseSearch(req);
|
return parseSearch(req);
|
||||||
@ -44,14 +44,16 @@ export class SimpleRouter {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 解析 req 和 res 请求
|
* 解析 req 和 res 请求
|
||||||
* @param req
|
* @param req
|
||||||
* @param res
|
* @param res
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
parse(req: Req, res: ServerResponse) {
|
parse(req: Req, res: ServerResponse) {
|
||||||
const { pathname } = new URL(req.url, 'http://localhost');
|
const { pathname } = new URL(req.url, 'http://localhost');
|
||||||
const method = req.method.toLowerCase();
|
const method = req.method.toLowerCase();
|
||||||
|
if (this.exclude.includes(pathname)) {
|
||||||
|
return 'is_exclude';
|
||||||
|
}
|
||||||
const route = this.routes.find((route) => {
|
const route = this.routes.find((route) => {
|
||||||
const matchResult = route.regexp.exec(pathname);
|
const matchResult = route.regexp.exec(pathname);
|
||||||
if (matchResult && route.method === method) {
|
if (matchResult && route.method === method) {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
import url from 'url';
|
import url from 'url';
|
||||||
|
|
||||||
export const parseBody = async (req: http.IncomingMessage) => {
|
export const parseBody = async <T = Record<string, any>>(req: http.IncomingMessage) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<T>((resolve, reject) => {
|
||||||
const arr: any[] = [];
|
const arr: any[] = [];
|
||||||
req.on('data', (chunk) => {
|
req.on('data', (chunk) => {
|
||||||
arr.push(chunk);
|
arr.push(chunk);
|
||||||
@ -12,7 +12,7 @@ export const parseBody = async (req: http.IncomingMessage) => {
|
|||||||
const body = Buffer.concat(arr).toString();
|
const body = Buffer.concat(arr).toString();
|
||||||
resolve(JSON.parse(body));
|
resolve(JSON.parse(body));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
resolve({});
|
resolve({} as T);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -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;
|
||||||
}
|
};
|
||||||
|
@ -147,7 +147,6 @@ export class Server {
|
|||||||
if (req.url === '/favicon.ico') {
|
if (req.url === '/favicon.ico') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res.headersSent) {
|
if (res.headersSent) {
|
||||||
// 程序已经在其他地方响应了
|
// 程序已经在其他地方响应了
|
||||||
return;
|
return;
|
||||||
@ -158,8 +157,6 @@ export class Server {
|
|||||||
// 交给其他监听处理
|
// 交给其他监听处理
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
||||||
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
||||||
if (cors) {
|
if (cors) {
|
||||||
res.setHeader('Access-Control-Allow-Origin', cors?.origin || '*'); // 允许所有域名的请求访问,可以根据需要设置具体的域名
|
res.setHeader('Access-Control-Allow-Origin', cors?.origin || '*'); // 允许所有域名的请求访问,可以根据需要设置具体的域名
|
||||||
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
|
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
|
||||||
@ -169,8 +166,6 @@ export class Server {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// res.writeHead(200); // 设置响应头,给予其他任何listen 知道headersSent,它已经被响应了
|
|
||||||
|
|
||||||
const url = req.url;
|
const url = req.url;
|
||||||
if (!url.startsWith(path)) {
|
if (!url.startsWith(path)) {
|
||||||
res.end(resultError(`not path:[${path}]`));
|
res.end(resultError(`not path:[${path}]`));
|
||||||
@ -184,8 +179,10 @@ export class Server {
|
|||||||
try {
|
try {
|
||||||
const end = await handle(messages as any, { req, res });
|
const end = await handle(messages as any, { req, res });
|
||||||
if (res.writableEnded) {
|
if (res.writableEnded) {
|
||||||
|
// 如果响应已经结束,则不进行任何操作
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||||
if (typeof end === 'string') {
|
if (typeof end === 'string') {
|
||||||
res.end(end);
|
res.end(end);
|
||||||
} else {
|
} else {
|
||||||
@ -193,6 +190,7 @@ export class Server {
|
|||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||||
if (e.code && typeof e.code === 'number') {
|
if (e.code && typeof e.code === 'number') {
|
||||||
res.end(resultError(e.message || `Router Server error`, e.code));
|
res.end(resultError(e.message || `Router Server error`, e.code));
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user