overcome CORS

This commit is contained in:
Михаил Капелько
2025-08-17 23:38:37 +03:00
parent 4c4a46ce28
commit 81f39d238f
2 changed files with 16 additions and 2 deletions

View File

@@ -7,7 +7,6 @@ let TEMPLATE_FILE_NAME = "%DIR%/consult_%UUID%";
let URL_CONSULT = "/api/consult"; let URL_CONSULT = "/api/consult";
let srv = http.createServer((req, res) => { let srv = http.createServer((req, res) => {
// POST
if (req.method == "POST") { if (req.method == "POST") {
var dat = ""; var dat = "";
req.on("data", (chunk) => { req.on("data", (chunk) => {
@@ -22,6 +21,8 @@ let srv = http.createServer((req, res) => {
returnError(res); returnError(res);
} }
}); });
} else if (req.method == "OPTIONS") {
returnNoCORS(res);
} else { } else {
returnError(res); returnError(res);
} }
@@ -58,6 +59,19 @@ function returnError(res) {
</html>`); </html>`);
} }
function returnNoCORS(res) {
res.writeHead(
204,
{
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
"Content-Length": "0",
"Content-Type": "text/plain;charset=UTF-8",
}
);
res.end();
}
function returnSuccess(res) { function returnSuccess(res) {
res.writeHead(200, { "Content-Type": "application/json;charset=UTF-8" }); res.writeHead(200, { "Content-Type": "application/json;charset=UTF-8" });
let dat = { code: 4 }; let dat = { code: 4 };

View File

@@ -34,7 +34,7 @@ server {
if ($request_method = "OPTIONS") { if ($request_method = "OPTIONS") {
add_header "Access-Control-Allow-Origin" "*"; add_header "Access-Control-Allow-Origin" "*";
add_header "Access-Control-Allow-Headers" "Content-Type"; add_header "Access-Control-Allow-Headers" "Content-Type";
add_header "Content-Type" "text/plain charset=UTF-8"; add_header "Content-Type" "text/plain;charset=UTF-8";
add_header "Content-Length" 0; add_header "Content-Length" 0;
return 204; return 204;
} }