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