0

Here are few lines of code.I did not understand their function.I have commented in the code the lines that I have not understood.

 var express = require('express');
 var app = express();
 var router = require('./app/router'); //not understood
 router(app); //not understood

It will be helpful I anyone can explain their function.

Aditya
  • 2,358
  • 6
  • 35
  • 61
  • 1
    They probably have a `router.js` file in an `api` folder in the project. They're requiring it and using the function. – Andrew Li May 12 '17 at 03:26
  • ok understood this..but In `router(app)` here app means the folder that is present named as `app ` where router file is present? – Aditya May 12 '17 at 03:30
  • @AndrewLi..Please answer this. – Aditya May 12 '17 at 03:39
  • @AdityaJain "*here app means the folder*" `api` would be the folder (`api` != `app`). In `router(app)`, `app` refers to an object – an instance of an [Application](https://expressjs.com/en/4x/api.html#app) created by the call to `express()`. – Jonathan Lonowski May 12 '17 at 05:19
  • @JonathanLonowski sorry for typo here in the question now it is `router(app)`.. – Aditya May 12 '17 at 05:52

2 Answers2

0

var router = require('./api/router'); //not understood

There is plenty of resources out there explaining this. See e.g. What is this Javascript "require"?

router(app); //not understood

router is a function returned by require('./api/router'). The function takes one parameter app.

What the router function does ? We can't know that as it is a proprietary code located in you filesystem in a ./api/router file.

Community
  • 1
  • 1
user272735
  • 10,473
  • 9
  • 65
  • 96
  • In `router(app)` here app means the folder that is present named as `app` where router file is present? Am I correct? – Aditya May 12 '17 at 03:43
  • @AdityaJain No. Your questions indicate that you needs to study more JavaScript basics. Please use your preferred JavaScript resources. `require` is not part of the JavaScript but my answer provides a good link to one resource to get you started. – user272735 May 12 '17 at 03:47
0

require function is the easiest way to include modules that exist in separate files.

usage file:

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

router function take app as a parameter for their use.

support (/app/router.js) file:

export default function(app) {
    // code stuff 
}
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133