07-21-2023, 03:12 PM
Let’s say your application is little complex. So what we do first is we divide the application into multiple modules so that changes in one module doesn't clutter the others and you can keep working on individual modules, but at the end of the day you need to integrate everything into one since you are building a single application. It is like we have one main application and few child applications whose parent is the main application.
So when we create the parent application we create one using
```js
const express = require("express");
const parent = express();
```
And to this parent application we need to bring in the child applications. But since the child applications are not totally different applications (since they run in the same context - java term), express provides the way to do it by means on the Express's Router function and this is what we do in the each child module file and lets call one such child module as `aboutme`.
```js
const express = require("express");
export const router = express.Router();
```
By `export` we are making this module available for other to consume and since we have modularized things we need to make the module files available to the parent application by means of node's require function just like any other third party modules and the parent file looks something like this:
```js
const express = require("express");
const parent = express();
const child = require("./aboutme");
```
After we make this child module available to the parent, we need to tell the parent application when to use this child application. Lets say when a user hits the path `aboutme` we need the child application about me to handle the request and we do it by using the Express's `use` method:
```js
parent.use("/aboutme", child);
```
and in one shot the parent file looks like this:
```js
const express = require("express");
const parent = express();
const child = require("./aboutme");
parent.use("/aboutme", child);
```
***Above all what the parent can do is it can start a server where as the child cannot.*** Hope this clarifies. For more information you can always look at the source code which takes some time but it gives you a lot of information.
So when we create the parent application we create one using
```js
const express = require("express");
const parent = express();
```
And to this parent application we need to bring in the child applications. But since the child applications are not totally different applications (since they run in the same context - java term), express provides the way to do it by means on the Express's Router function and this is what we do in the each child module file and lets call one such child module as `aboutme`.
```js
const express = require("express");
export const router = express.Router();
```
By `export` we are making this module available for other to consume and since we have modularized things we need to make the module files available to the parent application by means of node's require function just like any other third party modules and the parent file looks something like this:
```js
const express = require("express");
const parent = express();
const child = require("./aboutme");
```
After we make this child module available to the parent, we need to tell the parent application when to use this child application. Lets say when a user hits the path `aboutme` we need the child application about me to handle the request and we do it by using the Express's `use` method:
```js
parent.use("/aboutme", child);
```
and in one shot the parent file looks like this:
```js
const express = require("express");
const parent = express();
const child = require("./aboutme");
parent.use("/aboutme", child);
```
***Above all what the parent can do is it can start a server where as the child cannot.*** Hope this clarifies. For more information you can always look at the source code which takes some time but it gives you a lot of information.