What's the best way to separate the different tiers of an express application, so that my app.js file doesn't get crammed full of functions? I'm coming from a Java world, so I typically have my models, business logic and DAO code in separate tiers.
The other question, that has been bothering me: how do I open a connection to a DB in app.js and then share that among the various pieces of code that need access to it? Not the routed functions, but the business logic modules.
See this project as separate files as follows:
https://github.com/lethus/popbroker
routes.js - Here we put the routes, usually referenced to controllers
models.js - Model here you put the functions of MongoDB
forms.js - You work the validation of objects
controllers / users.js - That would be something like java UI, here we call the models.js and do the insert, update, list, Finds
Related
If one Nodejs app connects to a Mongo instance, and that app has defined a User schema with pre-save hooks, validation, etc.
And then another Nodejs app connects to the same database, and tries to register a User schema with different properties.
And then the second app saves a User
What happens?
I'm confused with how two Nodejs apps may communicate to the same database.
For example, it's very easy to see how one might want to have V2 of an api on a separate nodejs app developed by a separate team. But they will plug it into the same database and use the same Schema (or will they?), and I'm confused with how things are shared between the two apps.
Any help clarifying this in best-practices would be appreciated
I believe I've found the answer in the Documentation.
This connection object is then used to create and retrieve models. Models are always scoped to a single connection. docs
And
Models are fancy constructors compiled from our Schema definitions. docs
Which explains that a DB Connection 1's Schema Definitions (pre-save, etc), do not affect DB Connection 2's writes/etc.
Essentially, they are completely independent of validation and everything else. They only need to be OK in their own context.
I am having all the routes, methods, strategies, plugins and db connection in a single file 'server.js'. And this is working fine. But I want modular structure where the controllers, route, db are seperately defined. I used to modulate it, but stucking that how to call my multiple strategies in some of the routes and also how to call my db connection, the connection is established but I am unable to call it in the controllers. I am using hapi-mongodb plugin fo db connection. Please could anybody tell me how do I structure my files?
I use in my personal project the structure of start-hapiness project.
On this branch, have a simple example of a TODO list using Hapi + Mongoose and some cool plugins, modular and easily extensive!
https://github.com/thebergamo/start-hapiness/tree/dev-2.0
I think for modular MEAN application, angular-fullstack is best.
I want to create an object constructor for a new sign up within express. Where should this be located? Within the model? The controller?
It's really up to you to decide on your directory structure. Express doesn't enforce any particular structure.
That said, an object constructor clearly seems to fall into the model category. The model should include any data structures and logic for interacting with the database. The view is just what the user sees and interacts with, and the controller mediates between the view and the model (express routes would fit into this category).
For a short example using express with the sort of directory structure you seem to have, see this. Although the example uses a mongoose schema, the same principle should apply if you are using an ordinary object constructor. You might also want to look at some of the examples in the Express repository, including a different implementation of an mvc pattern.
How should I structure Node.js (Express) app? I do so:
app.js - starting the server
router.js - routing requests and starting functions from other files.
pages.js - render not-static pages. Example: request to database and render result
control.js - operation with alteration. Example: creation/updating line in database.
db.js - connection to database and creation model for ORM.
What should be changed? How to do better?
(Sorry for bad english)
Have you heard about Yeoman? It is a webapp generator, which is a very useful tool to automatically build webapp skeletons.
There are a lot of different generators of any web technology/framework you wish to use. Maybe a good idea can be having a look and exploring how these generators structure the code for you, which could give you an idea in how to keep your code organized.
You can Search for generators at Yeoman's site, just type Express and try different ones.
I am interested in creating a CRUD Rest API using Node.js together with express. I was wondering if their was a structuring standard of some sort or MVC framework which is used in the industry to structure my code create models etc...
I know that I should structure the different models into different npm projects but how to structure a single project is what I am looking for...
Also I am very interested in using a mediator design pattern to decrease coupling between the different modules.
Any examples/blogs/gits/books will help
Thanks
I did a screencast on this topic in which I propose an app structure that has served me well for APIs and projects with UIs. Do I claim this is gospel and then end-all-be-all? No. But it has served me well.
In short it looks something like this:
app_root
app
routes
- route handlers go here
models
- if you use models
commands
- if you use commands
middleware
- middlewarez
config
application.js - the stuff that bootstraps your application. Reusable in contexts other than your server (thing testing)
routes.js - All your route mappings in one place
test
test_helper.js - Bootstrap your testing (require config/application.js, etc)
models
- tests for your models (follow suit for other things under app)
server.js - starts up your webserver
I put this empty app structure up on GitHub here.
You could use Google's AngularJS + Node.JS where the angular frameworkwork separates view and controller. Look into Angular site for example and tutorial.