Как передать загруженный файл (составной) в njs nginx в цепочку запросов
Я пытаюсь передать загруженный файл из первого запроса во второй, сценарий таков: я делаю запрос с файлом в форме данных, после проверки я получаю некоторый ответ, затем отправляю файл и ответ на второй запрос.
Что я сделал:
1-попытаться получить файл с r.requestBuffer и вставить в тело.
2-попробуйте преобразовать r.requestText в файл с помощью fs.readFileSync, а затем поместите его в тело.
var fs = require("fs");
async function chain(r) {
var engine_uri = r.uri;
var args = r.args;
var body = r.requestBuffer; // fs.readFileSync(r.requestBuffer); // here i need to get file and send it to second request
r.headersOut["Content-Type"] = "application/json";
r.headersOut["Access-Control-Allow-Origin"] = "*";
r.headersOut["Access-Control-Allow-Methods"] = "*";
r.headersOut["Access-Control-Max-Age"] = "3600";
r.headersOut["Access-Control-Allow-Headers"] = "*";
if (r.method == "OPTIONS") {
r.return(200);
}
r.subrequest("/validation", { method: "POST" }) //first request
.then((Response) => {
if (Response.status <= 200) {
return JSON.parse(Response.responseText);
} else {
r.return(Response.status, JSON.stringify(Response));
}
})
.then((ResponseB) => {
r.subrequest("/persistance" + engine_uri, { //second request
args: `UserID=${ResponseB.UserID}&AccountID=${
ResponseB.AccountID
}&Username=${ResponseB.Username}&System=${
ResponseB.System
}&${jsonToQueryString(args)}`,
method: r.method,
body: body, // i need to transferre file to this request
}).then((resp) => {
r.return(resp.status, resp.responseText);
});
})
.catch((e) => {
r.return(200, e);
});
}
Также я поставил client_body_buffer_size 100M; в default.conf
js_import http.js;
server {
listen 80;
listen [::]:80;
server_name localhost;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
subrequest_output_buffer_size 5000k;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ ^/.*$ {
js_content http.chain;
}
location ^~ /persistance/ {
proxy_pass http://192.168.2.76:5001/;
}
location ^~ /AlphaCore/ {
proxy_pass http://192.168.2.76:8088;
}
location ^~ /AlphaComm/ {
js_content http.alpha_comm;
}
location ^~/AlphaCommRequest/ {
rewrite ^/AlphaCommRequest/(.*)$ /$1 break;
proxy_pass http://192.168.2.76:8586/AlphaComm;
}
location = /validation {
proxy_pass http://192.168.2.76:8088/AlphaCore/v1/user/validation;
}
client_body_buffer_size 100M;
}