What folder structure should I follow in Hapijs? - node.js

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.

Related

What is the best practise of using same Model on several projects?

I create a ReactJS app using Sequelize and PostgreSQL database. I defined my models on my ReactJS API, but I need to use the same database (so the same models) on an other API.
How should I do so without writing again the definition of each model, because if I made a change, I would have to do it everywhere. Is there a better way to handle this king of problem?
Why I don't use the ReactJS API? Because when I built my React app, the API disappears: I'm using proxy.
var myModel = require('./models/myModel');
you can add by require here my model is in root directory inside model folder.
You could place the models in their own separate Node.js package, so they could be reused by both.
Please, refer to this: https://docs.npmjs.com/getting-started/creating-node-modules

Mongoose Sharing Validation/Pre-Save Methods Across NodeJS Apps

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.

Bigger projects Node.js and RESTful API

I'm looking into node.js which really seem like a pretty nice environment. I've worked with a lot of different Technologies and for server, mainly php and Java (jsp), but dabbled in som RoR and Python.
I find node.js really easy to get up and running and it feels quite natural to work with, and I found some good entry level tutorials.
I just am missing some more intermediate resources. For example when creating bigger frameworks or api's how would you structure or architect it. I set up some smaller api's to try it out where it would go something like this:
I've made use of the Express framework to create a http server, listen to a port, set up an express object and bound some requests.
However these have been quite small, and the purpose has been learning, if I think about scaling up the size of the API for production, perhaps wanting to do other stuff like serve web-pages as well. I find it hard to see how the architecture would look.
It's vague as I am still new to node.js but I'm mainly thinking about things like if you typically keep all api in one file or if there are good ways to split it up into modules? And if anyone know any resource talking a bit more about how to design the architecture when working in node.js
Sorry for the vague question and thanks for reading.
In my opinion, Express is the good way to go if you want to build complex or big APIs.
It is among others easily testable (for instance with Mocha or Jasmine) and customizable, especially thanks to its middlewares.
For the directory structure, what I usually use is (at least) the following:
app.js : the main entrypoint. Will create the express application, indicate which controller to use for every route prefix, and assign the middlewares. Example from a previous project
controllers : will contain the controllers, the functions which will handle the requests, in the same style as in standard MVC frameworks (e.g. UserController, ...). Each controller would create an express Router object and export it. Inside the controllers, individual handlers are in charge of individual API requests, such as /api/users/list. It would use some library to access your data (e.g. Mongoose for MongoDB), and would then send the response to the client. Example (UserController.js)
models : will contain the models with all their attributes and methods. In my case, it would be the Mongoose models. Example (Song.js)
middlewares : will contain the various middlewares of the project. A practical example would be a middleware checking for an access token in the incoming request, and returning a 403 HTTP error if not. Example (AuthMiddleware.js)
helpers : various helpers
tests : unit tests of you API
This could be the minimal directory organization. On top of that, you may want to use a templating engine such as EJS to serve webpage. Take a look at « Use EJS to template your node application ».
This is only to give you an overview of what an express directory structure could look like, but there are of course plenty (better?) other possibilities. Hope that gives you a quick and useful insight :)

Structure Node.js (Express) app

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.

Separating models, logic and DAOs in express/node.js

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

Resources