router writeHead修改
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user