# nginScript (njs) feedback

**URL:** <https://www.ruby-forum.com/t/nginscript-njs-feedback/244834>\
**Category:** NGINX\
**Created:** [June 22, 2016, 8:36pm UTC](https://www.ruby-forum.com/t/nginscript-njs-feedback/244834 "2016-06-22T20:36:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [June 22, 2016, 8:36pm UTC](https://www.ruby-forum.com/t/nginscript-njs-feedback/244834/1 "2016-06-22T20:36:18Z")

</div>

I’m trying to implement a routing logic with nginScript. I haven’t been  
successful because I miss important javascript feature.

I would like to give you my feedback just in case it may help to improve  
njs (from more important/blocking to less important):

- There is no way to iterate through the keys of an object. It is not  
supported none of the following methods:
  - Object.keys
  - Object.getOwnPropertyNames()
  - for … in

- I would like to use/import nginx variables. Something like  
[[njs] Allow access to nginx vars via the njs $v variable](http://mailman.nginx.org/pipermail/nginx-devel/2016-May/008207.html)  
would  
be great.
- I miss string.split function. Probably map, reduce, and many other  
methods as well.
- It would be nice to have some “singleton” support where I could set  
a  
variable that is evaluated only once (first time is required or when  
nginx  
is started/reloaded). It’s clear that it should not be related to  
request/response but this approach would improve performance by  
avoiding  
executing same thing for each request. It would be similar to  
importing a  
javascript module (it’s only imported once and global vars and  
requirements  
are satisfied only once).
- I would like to include javascript modules. I’m using a directive:  
“include /etc/nginx/js/\*.js;” but this .js files need to include the  
js\_set  
$xxx “…” which makes the file to be invalid javascript (and you may  
miss  
some features from your javascript IDE).
- It would be nice to access a cookie by name (it wouldn’t be  
necessary  
if I could access to a nginx var like $v.cookie\_XXX).
- Logging function would be nice to help debugging.

Hope it can be helpful

---

<div class="post-metadata">

**Author:** ![leroj7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leroj7](https://www.ruby-forum.com/u/leroj7)\
**Post date:** [June 23, 2016, 12:59pm UTC](https://www.ruby-forum.com/t/nginscript-njs-feedback/244834/2 "2016-06-23T12:59:05Z")

</div>

I’ve continued with my attempt to integrate my routing logic as  
javascript  
code.

Regarding my previous feedback, it is possible to iterate the keys of an  
object with “for … in”. However, you cannot use “var” keyword to  
assign  
the result of the loop. For example, the following code fails:  
for (var i = 0; i \< elements.length; i++) {…}  
but the following one works:  
for (i = 0; i \< elements.length; i++) {…}

I’ve found a blocking point. There is a limitation of the size of the  
script. It could be solved easily if I could import other nginx vars. My  
module currently has 4202 chars (or 123 lines). This is really strange  
because I don’t consider it to be too big. The error raised is:

2016/06/23 11:48:42 [emerg] 5390#5390: too long parameter, probably  
missing  
terminating “”" character in /etc/nginx/js/routing.js:1

Is there any solution for this problem?

---

<div class="post-metadata">

**Author:** ![leroj7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leroj7](https://www.ruby-forum.com/u/leroj7)\
**Post date:** [June 24, 2016, 10:29am UTC](https://www.ruby-forum.com/t/nginscript-njs-feedback/244834/3 "2016-06-24T10:29:31Z")

</div>

On 23 Jun 2016, at 13:58, Jorge L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve continued with my attempt to integrate my routing logic as javascript code.
> 
> Regarding my previous feedback, it is possible to iterate the keys of an object  
> with “for … in”. However, you cannot use “var” keyword to assign the result of  
> the loop. For example, the following code fails:  
> for (var i = 0; i \< elements.length; i++) {…}  
> but the following one works:  
> for (i = 0; i \< elements.length; i++) {}

Yes, var declaration inside for is not currently supported, it will be  
fixed soon.

> I’ve found a blocking point. There is a limitation of the size of the script. It  
> could be solved easily if I could import other nginx vars. My module currently has  
> 4202 chars (or 123 lines). This is really strange because I don’t consider it to  
> be too big. The error raised is:
> 
> 2016/06/23 11:48:42 [emerg] 5390#5390: too long parameter, probably missing  
> terminating “”" character in /etc/nginx/js/routing.js:1
> 
> Is there any solution for this problem?

Currently no. The string parameter length is limited by 4K. We are going  
to support js in files.

> On Wed, Jun 22, 2016 at 8:35 PM, Jorge L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> I’m trying to implement a routing logic with nginScript. I haven’t been  
> successful because I miss important javascript feature.
> 
> I would like to give you my feedback just in case it may help to improve njs  
> (from more important/blocking to less important):
> 
> There is no way to iterate through the keys of an object. It is not supported  
> none of the following methods:  
> Object.keys  
> Object.getOwnPropertyNames()  
> for … in  
> I would like to use/import nginx variables. Something like  
> [[njs] Allow access to nginx vars via the njs $v variable](http://mailman.nginx.org/pipermail/nginx-devel/2016-May/008207.html) would be  
> great.  
> We consider another interface: req.variables object.  
> I miss string.split function. Probably map, reduce, and many other methods as  
> well.  
> It would be nice to have some “singleton” support where I could set a variable  
> that is evaluated only once (first time is required or when nginx is  
> started/reloaded). It’s clear that it should not be related to request/response  
> but this approach would improve performance by avoiding executing same thing for  
> each request. It would be similar to importing a javascript module (it’s only  
> imported once and global vars and requirements are satisfied only once).  
> I would like to include javascript modules. I’m using a directive: “include  
> /etc/nginx/js/\*.js;” but this .js files need to include the js\_set $xxx “…”  
> which makes the file to be invalid javascript (and you may miss some features from  
> your javascript IDE).  
> It would be nice to access a cookie by name (it wouldn’t be necessary if I could  
> access to a nginx var like $v.cookie\_XXX).  
> We consider another interface: req.cookies object.  
> Logging function would be nice to help debugging.  
> Hope it can be helpful

Thank you for your feedback!  
The full String object support, global objects, logging, and files are  
on the way.

---

<div class="post-metadata">

**Author:** ![leroj7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leroj7](https://www.ruby-forum.com/u/leroj7)\
**Post date:** [June 24, 2016, 6:47pm UTC](https://www.ruby-forum.com/t/nginscript-njs-feedback/244834/4 "2016-06-24T18:47:48Z")

</div>

​Thanks for your reply. It sounds great!
