head & content-length (wip)

This commit is contained in:
Flummi 2023-04-26 15:13:38 +02:00
parent 0cd49572a6
commit 4edafbcb7b

View File

@ -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);