Njs - r.variables does not contain variables declared by js_var

王明君 wmjhappy_ok at 126.com
Wed Apr 6 07:02:25 UTC 2022


Hi, sorry to bother again ~ 
I was trying to access variables declared using js_var but failed. Confused why is that.


This is a test demo, but actually i have a massive nginx config file that i want to minify using njs, and under which circumstance i need the js script to have access to variables declared in nginx.conf, either by using js_var directive, or something else (not found).


demo - nginx.conf
----------------------------------
http {
js_import main from main.js;
js_var $abc "ss fd";


server {
listen 80;
server_name localhost;


location / {
js_content main.hello;
}
}
}


demo - main.js
----------------------------------
function hello(r) {
       r.headersOut["Content-Type"] = "application/json";
       var m = {};
       for(var p in r)
               m[p] = r[p];
       m["variables"] = r.variables;
       r.return(200, JSON.stringify(m));
}

export default { hello };
---------------------------------------


demo - output






=======================



uname -a
Linux localhost.localdomain 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux


cat os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

|
|
|
|
|
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20220406/97dac71d/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: EE79CD01-959B-4103-AC51-0CF482A91F88.png
Type: image/png
Size: 7776 bytes
Desc: not available
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20220406/97dac71d/attachment.png>


More information about the nginx mailing list