Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 698 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does double parentheses mean in a require

#1
I know what this require statement does.

var express = require('express');
var app = express();

But sometimes i have seen two parentheses after the require.

var routes = require('./routes')(app);

**Q)** What does this mean, and how does it work?
Reply

#2
That mean that behind that, there is a function that is exported using `module.exports`:

module.exports = function(app) {
app.get("/", function(req, res){});
}

See also

[To see links please register here]


Sidenote:

You could create function on the fly:

A.js

module.exports = function(data) {
return function(req, res, next) {
// Do something with data
next();

}

main.js

...
app.use(require("A")(data));
...
Reply

#3
Well, [require](

[To see links please register here]

) is a function provided by Node.js that basically loads a module for you and it returns whatever you expose in the module that you loaded.

If what you expose (through the use of module.exports) in a given module is a function, then that is what requires returns. For instance.

//moduleX.js
module.exports = function(){
return "Helo World";
}

Then if you require it, you get a function back

var f = require('./moduleX');
console.log(f()); //hello world

Of course, you could invoke the function directly once you require it.

var greet = require('./moduleX')();
console.log(greet);


Reply

#4
This is a pattern in which the [`module.exports`][1] of the module you are requiring is set to a function. [Requiring][2] that module returns a function, and the parentheses after the require evaluates the function with an argument.

In your example above, your `./routes/index.js` file would look something like the following:

module.exports = function(app) {
app.get('/', function(req, res) {

});
// ...
};

This pattern is often used to pass variables to modules, as can bee seen above with the `app` variable.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through