简体   繁体   中英

questions about node.js application launch

I've received a node.js application to put in a production environment I'm responsible for (I'm the unix admin). The main file (which launches the application is as follows):

var module1 = (require('./module1.js')), ls;
var module2 = (require('./module2.js')), rd;
var module3 = (require('./module3.js')), rs;
var module4 = (require('./module4.js')), rd;
var module5 = (require('./module5.js')), rd;
var module6 = (require('./module6.js')), rd;
var module7 = (require('./module7.js')), rd;

For curiosity (I'm not a javascript / node.js programmer) I tried to lookup the syntax on the internet. The closest I found was var module = require('./module.js'); but nothing that has the enclosing parenthesis and the rd /rs / ls suffix......

What is the purpose of putting 'require...' inside parenthesis and adding the suffixes: rd, rs, ls?

a couple of clarifications: 1. I asked one of the programmers and he told me that each module launches a different web application (using Express module). 2. The original programmer that wrote this part isn't working with the company anymore, isn't reachable and no one wants to touch this part of the application :-(

Understanding Syntax

In JavaScript when you declare variables using var you can declare several variables at once delimited by a comma ( , ).

For example:

var v1 = "example", v2 = "example2", v3;

v1 , v2 both exists and has a value. v3 exists but its value will be undefined.

Applying it in your case

In your case, you have:

var module1 = (require('./module1.js')), ls;

I don't know what require('./module1.js') returns, but for the sake of simplicity, let's say it contains module.exports = "abc" , which means require('./module1.js') will return "abc" .

This means this line will essentially be:

var module1 = "abc", ls;

module1 would have a value of "abc" , and ls will be value of undefined .

Now the fact that you have a repeating suffix is indeed strange and doesn't appear to serve any real purpose. It definitely does not follow the typical pattern of including modules. They would just simply be variables holding a value of undefined (from what is shown in the provided code). Perhaps they are used elsewhere in the code base.

Edit

I asked one of the programmers and he told me that each module launches a different web application (using Express module)

By default in NodeJS, each module has a module.exports object which is simply just an empty object {} . If inside those modules that are required, it doesn't reassign or add onto module.exports , then the require will return only an empty object.

However, the module is still executed, so the pattern (some may call it an anti-pattern) is side effect modules. Where the required modules serves no APIs or purpose other than to cause a side effect, which in this case starting an Express app, and likely binding a listening connection to different ports.

The author could have written

require('./module1.js');
require('./module2.js');

to achieve the same result. Of course however, without the var keyword, they couldn't declare ls , rs , and rd at the same time, which might be the reason why the author to write the way he did. It is still unclear the significance or the purpose of these variables given the provided code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM