Folder Structure to Also Include Server-Side JS


#1

Continuing the discussion from How can I have one file per controller?:

@gregorypratt (and @Calendee) I really like how you’ve structured your JS code - very clean, easy to see at a glance where everything is and its purpose. Great design!

Being completely new to Ionic, my question is how to expand this structure to also include server-side JS - basically all the Node.js code that handles the server side of my application.

Would it make sense to create two folders directly under js — one client and the other server — and have all the client-side code in this client folder(@gregorypratt, all your folders currently under js would now be under client), and then the Node.js code under the server folder? Something like this:

Or, based on your experiences, would another folder structure be more optimal in accommodating both client- and server-side code?

Thanks a lot for sharing your ideas — very helpful!


#2

As Ionic is intended for hybrid mobile apps, I would not include ANY server side code in the app structure. You don’t want any of that code compiled into your app and made publicly available.

If you intend to run this entire application from the server and just have a “web app”, I’d still recommend separating it out completely. Imagine the app is a standalone entity that is accessing some server resources out of your control. So, you’re just doing API calls to it.

Then, set up your server side code to be the API for the app in a completely different location.


#3

@Calendee: yes, that makes sense. I forgot to mention that I’d want the server code actually outside of www because i would not want it compiled into the app, and that’s where the app builder looks for the code (in www, right?) So what you say makes sense. But, to keep things organized - even if I only access the server through a REST-type setup (e.g., to access a database), I’d still like to keep a server.js file with route handling, etc. somewhere as part of the project. So presumably if I kept it out of www, I’d be OK with that yes? Like, say, in a folder at the same level as www, under the project. That wouldn’t interfere with the Cordova build process, would it?

Thanks for pointing an Ionic n00b in the right direction!


#4

As long as your server code doesn’t get wrapped up in the cordova project, I guess it would be okay.


#5

Hi @oaksagelew I would probably advice against keeping your server and client code in the same location. As @Calendee says you don’t want to manage the overlap and they are separate entities.

Also technically they shouldn’t even be aware of each other. Keeping a clear separation of concerns between code extends to directory structure too IMO. You could at a later date replace your Node code with something totally different whist keeping your REST endpoints. Or build another consuming application. They are not connected so shouldn’t live together. Decouple as much as possible :wink:

Hope we’ve helped!


#6

Got it. Thank you - the “separation of concerns” is by far the strongest argument of all. And, as you, @gregorypratt, say, if in a year from now I decide to swap out the current Node-based back end for some other back-end technology, having the two aspects separated physically but connected via a REST API (which shouldn’t need to change) will make that swapping task much easier.

Yes, you have helped a lot - thank you very much for taking the time to reply; I appreciate it.