From 4edafbcb7b284fac7cecb0b7f2765059db65b506 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 26 Apr 2023 15:13:38 +0200 Subject: [PATCH] head & content-length (wip) --- src/index.mjs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index 37dd5b8..9403222 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -53,8 +53,8 @@ export default class flummpress { await Promise.all([...this.middleware].map(m => m(req, res))); - const method = req.method.toLowerCase(); - const route = this.router.getRoute(req.url.pathname, req.method); + const method = req.method === 'HEAD' ? 'get' : req.method.toLowerCase(); + const route = this.router.getRoute(req.url.pathname, req.method == 'HEAD' ? 'GET' : req.method); if(route) { // 200 const cb = route[1][method]; @@ -107,7 +107,16 @@ export default class flummpress { code = 200, type = "text/html", body - }) => res.writeHead(code, { "Content-Type": `${type}; charset=utf-8` }).end(body); + }) => { + res.writeHead(code, { + "content-type": `${type}; charset=UTF-8`, + "content-length": body.length + }); + if(res.method === 'HEAD') + body = null; + res.end(body); + return res; + }; res.json = (body, code = 200) => { if(typeof body === 'object') body = JSON.stringify(body);