refactor request handling to replace parsedUrl with url for consistency and improve type definitions
This commit is contained in:
parent
4514a37999
commit
3aa7673d5d
12
src/index.ts
12
src/index.ts
@ -62,7 +62,7 @@ export default class Flummpress {
|
|||||||
await mw(req, res, () => {});
|
await mw(req, res, () => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
const route = this.router.getRoute(req.parsedUrl.pathname, req.method!);
|
const route = this.router.getRoute(req.url.pathname, req.method!);
|
||||||
|
|
||||||
if(route) {
|
if(route) {
|
||||||
const [pathPattern, methods] = route;
|
const [pathPattern, methods] = route;
|
||||||
@ -75,7 +75,7 @@ export default class Flummpress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(handler) {
|
if(handler) {
|
||||||
req.params = req.parsedUrl.pathname.match(new RegExp(pathPattern))?.groups || {};
|
req.params = req.url.pathname.match(new RegExp(pathPattern))?.groups || {};
|
||||||
req.post = await this.readBody(req);
|
req.post = await this.readBody(req);
|
||||||
handler(req, res);
|
handler(req, res);
|
||||||
}
|
}
|
||||||
@ -95,7 +95,7 @@ export default class Flummpress {
|
|||||||
`[${new Date().toISOString()}]`,
|
`[${new Date().toISOString()}]`,
|
||||||
`${(process.hrtime(start)[1] / 1e6).toFixed(2)}ms`,
|
`${(process.hrtime(start)[1] / 1e6).toFixed(2)}ms`,
|
||||||
`${req.method} ${res.statusCode}`,
|
`${req.method} ${res.statusCode}`,
|
||||||
req.parsedUrl.pathname,
|
req.url.pathname,
|
||||||
].join(" | "));
|
].join(" | "));
|
||||||
}).listen(...args);
|
}).listen(...args);
|
||||||
|
|
||||||
@ -104,8 +104,8 @@ export default class Flummpress {
|
|||||||
|
|
||||||
private parseRequest(request: IncomingMessage): Request {
|
private parseRequest(request: IncomingMessage): Request {
|
||||||
const url = new URL(request.url!.replace(/(?!^.)(\/+)?$/, ""), "http://localhost");
|
const url = new URL(request.url!.replace(/(?!^.)(\/+)?$/, ""), "http://localhost");
|
||||||
const req = request as Request;
|
const req = request as unknown as Request;
|
||||||
req.parsedUrl = {
|
req.url = {
|
||||||
pathname: url.pathname,
|
pathname: url.pathname,
|
||||||
split: url.pathname.split("/").slice(1),
|
split: url.pathname.split("/").slice(1),
|
||||||
searchParams: url.searchParams,
|
searchParams: url.searchParams,
|
||||||
@ -153,7 +153,7 @@ export default class Flummpress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private createResponse(response: ServerResponse): Response {
|
private createResponse(response: ServerResponse): Response {
|
||||||
const res = response as Response;
|
const res: Response = response as Response;
|
||||||
|
|
||||||
res.reply = ({ code = 200, type = "text/html", body }) => {
|
res.reply = ({ code = 200, type = "text/html", body }) => {
|
||||||
res.writeHead(code, { "Content-Type": `${type}; charset=utf-8` });
|
res.writeHead(code, { "Content-Type": `${type}; charset=utf-8` });
|
||||||
|
@ -234,7 +234,7 @@ export default class Router {
|
|||||||
|
|
||||||
this.get(route, (req: Request, res: any) => {
|
this.get(route, (req: Request, res: any) => {
|
||||||
try {
|
try {
|
||||||
const filename = req.parsedUrl.pathname.replace(route, "") || "index.html";
|
const filename = req.url.pathname.replace(route, "") || "index.html";
|
||||||
const mime = this.mimes.get(filename.split(".").pop() || "");
|
const mime = this.mimes.get(filename.split(".").pop() || "");
|
||||||
const file = path.join(dir, filename);
|
const file = path.join(dir, filename);
|
||||||
const stat = fs.statSync(file);
|
const stat = fs.statSync(file);
|
||||||
|
7
src/types.d.ts
vendored
7
src/types.d.ts
vendored
@ -1,7 +1,8 @@
|
|||||||
import { IncomingMessage, ServerResponse } from "node:http";
|
import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from "node:http";
|
||||||
|
import { Socket } from "node:net";
|
||||||
|
|
||||||
export interface Request extends IncomingMessage {
|
export interface Request extends Omit<IncomingMessage, 'url'> {
|
||||||
parsedUrl: {
|
url: {
|
||||||
pathname: string;
|
pathname: string;
|
||||||
split: string[];
|
split: string[];
|
||||||
searchParams: URLSearchParams;
|
searchParams: URLSearchParams;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user