Return correct content-type and HTTP code for errors

master
Vitaliy Filippov 2024-05-09 13:49:39 +03:00
parent b4e0ebd600
commit 2e89aa8b17
1 changed files with 4 additions and 2 deletions

View File

@ -122,6 +122,7 @@ class AntiEtcd
data = Buffer.concat(data);
let body = '';
let code = 200;
let ctype = 'text/plain; charset=utf-8';
let reply;
try
{
@ -144,6 +145,7 @@ class AntiEtcd
}
reply = await this.runHandler(req, data, res);
reply = JSON.stringify(reply);
ctype = 'application/json';
}
catch (e)
{
@ -169,8 +171,8 @@ class AntiEtcd
'\n '+reply.replace(/\n/g, '\\n')
);
reply = Buffer.from(reply);
res.writeHead(200, {
'Content-Type': 'application/json',
res.writeHead(code, {
'Content-Type': ctype,
'Content-Length': reply.length,
});
res.write(reply);