Wednesday 18 December 2019

Does my app get bigger when I split my code to many files and I import modules in every file I use?

Hello!I have started learning NodeJS and I'm doing my experiments and developing a habit of splitting my code to many many files so that I when I have to change something I will do it only on a particular file and avoid too many lines of code, let me give you an example: routers.My web app has routers and one of them is called /about_us which is a parent and has the following children:/about_us/team/about_us/members/:id/about_us/mission/about_us/locationAnd each of them I have them split in multiple files.about_us_mainRouter.jsabout_us (folder)team.jsmembers.jsmissionlocationand there are times when I want to make some specific changes, for example, members I change only the members file.My router code is like this:about_us_mainRouter.js const express = require('express'), aboutUs = express(), members = require('./about_us/members'), mission = require('./about_us/mission'), location = require('./about_us/location'); aboutUs .use('/members', members) .use('/mission', mission) .use('/location', location); module.exports = aboutUs; and a file which gets imported is like this:members.jsconst express = require('express'), members = express.Router(), membersController = require('/controllers/about_me/members'); members.route('/:memberId') .get(membersController.retrieve) module.exports = members; As you can see in every router, I import express and express Routers and I've been wondering:Does my app, loads express() or express.Router() for every import I do? If I have for example imported express 100 times (if I have 100 Routers) does JavaScript/NodeJS load the module 100 times?Is there any way of skipping the imports, since I have imported them in other files, and making my code shorter even more?Thanks

Submitted December 18, 2019 at 11:45AM by MariosFFX

No comments:

Post a Comment