Fixing HTTP status code not correctly outputed because of CORS headers

This commit is contained in:
David Négrier 2020-10-15 17:27:40 +02:00
parent e7890907c1
commit 2bf795d9c2
3 changed files with 40 additions and 22 deletions

View file

@ -44,8 +44,6 @@ export class FileController extends BaseController {
this.App.post("/upload-audio-message", (res: HttpResponse, req: HttpRequest) => {
(async () => {
this.addCorsHeaders(res);
res.onAborted(() => {
console.warn('upload-audio-message request was aborted');
})
@ -80,14 +78,18 @@ export class FileController extends BaseController {
}
});
res.writeStatus("200 OK").end(JSON.stringify({
res.writeStatus("200 OK");
this.addCorsHeaders(res);
res.end(JSON.stringify({
id: audioMessageId,
path: `/download-audio-message/${audioMessageId}`
}));
} catch (e) {
console.log("An error happened", e)
res.writeStatus(e.status || "500 Internal Server Error").end('An error happened');
res.writeStatus(e.status || "500 Internal Server Error");
this.addCorsHeaders(res);
res.end('An error happened');
}
})();
});
@ -101,7 +103,6 @@ export class FileController extends BaseController {
});
this.App.get("/download-audio-message/:id", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.onAborted(() => {
console.warn('upload-audio-message request was aborted');
@ -111,7 +112,9 @@ export class FileController extends BaseController {
const file = this.uploadedFileBuffers.get(id);
if (file === undefined) {
res.writeStatus("404 Not found").end("Cannot find file");
res.writeStatus("404 Not found");
this.addCorsHeaders(res);
res.end("Cannot find file");
return;
}