How can I properly structure my NodeJS, Express, and MongoDB code? - node.js

I am new to all three but I do know javascript well. I understand basics of them all but do not understand what the best way to implement the NodeJS server javascript with the express MVC and MongoDB database querys. I want to break up all this into their own files obviously but I am not sure how everything fits into the pre-created project template created by Express. Does anyone have a good solution or resource to share with me? Thank you

I recently started with node.js, express and mongodb myself and it's true the resources out there are mostly outdated. First of all, express is not an mvc framework. It has no opinion on the structure which you'll find out that it makes it so great. Your question is how to organise the code? It's clearly a personal preference but you can create a model folder where you can put your db queries for specific resources, your lib folder where you can establish a connection to your mongodb instance and call it wherever you need it and use the routes folder for your more specific needs like controllers on a traditional MVC. Another thing is that you have to decide how to interact with mongo. You can either use the native mongodb driver, perhaps with mongoskin as suggested, or mongoose for easier interactions such as defining schemas. Personally i prefer the native but it took me a while to get what it does and how it works. Finally for resources what i can suggest from personal experience and it's still my own opinion, is that the best thing you can do is buy the book Node.js in Action which was what made me realize what node.js is all about and how to use it. Hope it helps...

Related

Using p5.js, node.js and mongodb for a complete website

I want to create a small mini-mart website using all the javascript codes, p5.js for frontend, node.js for backend and mongodb for the database.
Is it good to work with all the javascript codes?
if not, what are other js to work on?
You can definitely do it, although P5 is not that great for frontend... Node.js is good, but unless you just want to build an HTTP server from scratch with the default node.js API you should use something like Express. Also, you should use something like Mongoose for interaction with the Mongo database, since it usually makes everything much easier!

Is there a way to self-host mongoose databases?

I am getting started with writing an API for a project and the tutorial I am following suggests I sign up for a hosted solution. I think that is ridiculous. My project is simple and I do not feel the need to be locked in to a service. If it helps, I am using Express.
Mongoose is a node.js module (library) which is used to interact with a database, called MongoDB.
There are some websites like mongolab.com which offer plans for development for free, so you would jsut need to sign up and you will get a database without installink anything in your computer/server etc. This is why they say it is easier.
You can install MongoDB in your local computer to test (I think most of us have it) and use just that one for developing and testing.
To install MongoDB it all depends which Operating System you are using at the moment. But you can look up on google: "Install MongoDB MacOSX/Window/Ubuntu/etc.." and normally is just one simple command. To connect to it in your local you don't need a user or anything I guess.

Building one page apps with angularjs and sails.js backend

I just found out about sails.js and i think it's really awesome. I want to build apps with sails and angular, but i haven't found any guides on doing this. I found this site which is supposed to have a talk on using sails.js instead on express in the mean stack, but there's not video there.
I would be glad for links to videos or blog posts on how to do this.
Thanks all.
This is getting downvoted because its not a direct question about code.
I have a boilerplate sailsjs app that you can use to model your own off of..
https://github.com/mikedevita/sailsjs-v10-angularjs-requirejs-boilerplate
This is based on v0.10 which is in beta right now, but the same concept applies.
I've created a simple yet powerful angular service, published as a bower component: https://github.com/diegopamio/angular-sails-bind
It basically:
Creates the model inside the $scope for you.
Retrieves the model data from the backend using socket.
Watches for changes made by the user in the UI and updates the backend immediately.
Watches for changes made in the backend and updates the UI immediately.
And it does it with just one line of code in your angular app, like this: $sailsBind("", $scope);
I made it as part of a personal project and then I decided to publish it as it could benefit others. It works with sails 0.10. Let me know if it suits your needs or if you'd like to see something else on it.

Creating modules/HMVC in sails.js

I am trying to make a separation in my new SailsJS app by creating modules/HMVC, just like ASP.NET MVC, and Codeigniter PHP. These frameworks provide a mechanism for handling such a separation. Do any one have an idea how to implement this in SailsJS?
There is some development taking place in that direction, I'm not quite sure whether or not anything usable already exists, at least I haven't been able to find anything final so far. Two threads where adding HMVC to sails was discussed are
https://github.com/balderdashy/sails/issues/1191
https://github.com/balderdashy/sails/issues/594
I think the most recent sails plugin/backend components system discussion is balderdashy/sails/pull/2083.
It looks like the most viable module solution today is balderdashy/sails-hook-subapps, an example of a sails hook. Hopefully, it works with 0.12.x and gets some attention/docs soon!
Additionally, the project sailorjs/sailorjs appears to do exactly this as a fork of sails from version 0.10.x, though I don't know if there are plans ot utilize that work in sails.js.

Re-using backbone routes, models, and views on the node.js server

I am trying to build an application which both works as a SPA and as a normal application without re-writing any of the code as much as possible. My research has led to believe that this could be very possible using node + backbone (on both the server and client). I have found some resources on how to do this but nothing truly complete. Have anyone tried a full solution to re-using all the code?
The resources that I found:
Re-using backbone models on the server: http://blog.andyet.com/2011/feb/15/re-using-backbonejs-models-on-the-server-with-node/
Re-using backbone views and layout manager on the server:
http://vimeo.com/46033641
I have also found some frameworks that achieve this such as (bones, derby, meteor), but I dont feel like using such a framework is the best solution since I still want to have full control over backbone and node.
What you are trying to implement is Resource-View-Presenter model. Please read this article completely http://blog.nodejitsu.com/scaling-isomorphic-javascript-code. It provides much needed insight for isomorphic code that can execute both on the client and the server. It also helped me understand various models and architectures.
What backbone.js uses is traditional MVC framework, which cannot work on both client and server without modifications. So if you want to stick to backbone.js you will face this disadvantage sooner or later. I tried backbone too and found no easy way to do that.
I tried looking for RVP frameworks on node.js, but it is still new. I would suggest you look into flatiron.js http://flatironjs.org/. It is based on Resource-View-Presenter model, the only one I could find for node.js. Some changes will be required to implement on both server/client, but it is not much. You will find it is similar to backbone.
Hope it helps you to find the right path.
I recently did some research on the field and found some interesting projects that are using the same backbone code on the client and server (nodejs).
Project 1
The first one was introduced by Keith Norma from Groupon. He managed to reuse backbone router + templates on the client and server.
App demo: https://github.com/keithnorm/SpainJS-Pipedream-Demo
Presentation at SpainJS talk: http://www.youtube.com/watch?v=jbn9c_yfuoM
Project 2
The second project is Rendr presented by Spike Brehm from Airbnb but not released yet to the public. It seems a bit more ambitious as it also trades backbone model and collections to the nodejs server. You can read more about it here: http://nerds.airbnb.com/weve-launched-our-first-nodejs-app-to-product
UPDATE: Rendr code has been released: https://github.com/airbnb/rendr
Project 3
There is another project that also allows to share backbone code between client and server: https://github.com/developmentseed/bones

Resources