![]() |
|
[Javascript] Trying to create a 'modular' structure in my project - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183) +---- Thread: [Javascript] Trying to create a 'modular' structure in my project (/showthread.php?tid=6040483) |
Trying to create a 'modular' structure in my project - VirtualFrost - Jan 12, 2020 I'm starting a new project and want to try create this kind of 'modular' style to it. So far I've just used promises to load each file and was looking for some input into if there is any way to improve this. Basically the app will start, and it will start loading each module, it should load each app, if any fail then it'll output the error and then terminate the process. If there's anything I could do better or maybe a simpler way I'd love the input Below is the basic structure app.js /modules/index.js /modules/test1.js /modules/test2.js app.js Code global.server = {};modules/index.js Code server.test1 = require('./test1.js');test1.js Code module.exports = {Ignore the code inside test1.js, it's all for testing, all of this code works fine as is and will successfully return each resolve when they've all done loading. Only looking to see if this structure and loading can be done any easier/simpler than what I've done here. RE: Trying to create a 'modular' structure in my project - doon - Jan 13, 2020 Nice I like this pattern. I use a similar pattern for having multiple services + adding new services needed for the app. One thing I do since someone could add a new module is load modules from a config somewhat like this. PHP Code Then load the services like this PHP Code Not sure if this could be relevant to your project but works well for adding modules easily. FYI I use app-root-path for getting root app path to avoid having to ../../ directories. RE: Trying to create a 'modular' structure in my project - DrDoof - Jan 13, 2020 Code const fs = require('fs')Why not just have it load all "modules" in the directory rather than having to add new files to a list/config file? RE: Trying to create a 'modular' structure in my project - VirtualFrost - Jan 14, 2020 (Jan 13, 2020 11:57 AM)DrDoof Wrote: Actually this is interesting, I might play around with how this is done. I did want to avoid having to manually write in each file name into a list as I know it's just annoying to do. I appreciate this a lot! |