I'm currently working on a web application based on the popular MEAN stack (MongoDB, Express, AngularJS and Node.js). As I am still struggling with some aspects of node.js, I would like to ask for your advice and opinion on a major architecure decision of mine.
As I found Jade rather ugly (notation-wise) and did not want the server to render webpages (for cpu power saving reasons), I chose to always send back prepared html files/templates.
Example:
app.get('/', function(req, res) {
res.sendfile("public/index.html");
});
On the client side, I then initiate further Angular-JSON based communication to realize REST webservices to instantly get and fill in the needed data. Any reasonable arguments against this approach or maybe good points why I should be using a render engine on the server side?
Besides, does anyone know a good online tutorial for a multipage web application featuring user registration/validation, authentication process, rest webservices and so on? Something newbies can take a look at to get a feeling of how to structure a medium to large sized node.js application well? I've been googling for days now but feel that all tutorials I found were always missing essential ingredients or were rather "elementary".
Thanks in advance.
Igor
I started with Node.js like 2 month ago and now I am starting a project following this same approach.
In the same way, I am not using a "render engine", mainly because I am planning to use my client code (angular) to create a hybrid application using cordova (phoneGap).
I learned a lot from the tutorials of Node.js in Pluralshigh:
http://pluralsight.com/training/Courses/TableOfContents/building-angularjs-nodejs-apps-mean
http://pluralsight.com/training/Courses/TableOfContents/nodejs-dotnet-developers
Finally, regarding security, "Passport" is a great library to implement authentication.
btw: I am using Token-Based Authentication.
And the answer to "how integrate Passport with tokens", I found it here:
passport-local with node-jwt-simple
Related
I've been getting into Node.js recently and I really like the example shown on Socket.io's website showing a real time chat application. However, in all of the examples I see the URL used is localhost:8080 or whatever ip and port is applicable. However, is there any way to implement this into an existing webpage so that it isn't a separate URL? If not, how are these real-time chat apps done in real life?
these sort of questions are usually not meant for stackoverflow, but everyone has been where you are! Stackoverflow is dedicated for code related questions, but this might help you get to your next steps:
NodeJS is a backend server language, you will have to deploy it to a production environment.
New developers typically learn by using services like heroku.com to quickly host backend apps, they take a lot of the complexity of learning Linux or Microrost Server out of the mix, but they are not typically used by more advanced developers.
More advanced (real world apps) run on AWS/Azure/Other hosts.
Implementing it in an existing website requires both frontend and backend languages. Nodejs gives you a leg up in that it's javascript, and can run in both frontend and backend situations.
There are many places online that provide tutorials for creating backend servers and frontend websites.
the list is endless, but some good places for new developers are
https://frontendmasters.com/
https://teamtreehouse.com/
https://egghead.io/
Here is a tutorial on heroku for setting up a nodejs app
Getting Started on Heroku with Node.js
I have examined and practiced on creating Rest Api's with loopback and ExpressJS seperately
While Using Loopback;
It was really time consuming to read all the documentation and learning loopback specific stuff
It also enables you to create your Api in a short period of time and has lots of magic things inside it.
I saw that if I face problems while developing with loopback. I usually get stuck in finding answers from community.
While Using ExpressJs
You write almost every api endpoint in same format with lots of copied code.
You are comfortable with it and can do anything with ease.
But time consuming if compared with loopback.
My point is to utilize best parts of both ExpressJs and Loopback
So my question is "Does it make sense to use Loopback with ExpressJs and also use Mongoose?"
Related with my question Loopback has a documentation about adding ExpressJS route in Loopback application. Loopback with Express Route
If it makes sense is there any recommendation for folder structuring ?
As #Jspdown wrote in his comment to your question Loopback is really based on Express, so you don't need to make choice between them.
When you work on your application using Express and produce 'lots of copied code', soon you start to optimize it and in fact develop your own framework. So this is exactly what Loopback team already have done for you.
Thus the choice is not Loopback or Express but it is - to invest into study ready-made framework or to develop your own framework. As for me I think that if you are working on relatively simple projects and/or quick prototypes or going to change your programming tools soon - don't study, just do. But if you are working on large, mission critical system in long-lasting project - Loopback is helpful.
I am asked to project a web solution to allow vote casting in public assemblies.
The solution shall be written with node.js on the server, and javascript, possibly using angular.js on the client (since javascript is the language I know best, today :-).
The app shall run on mobile devices, and will be quite simple: it shall handle some sort of authentication, to identify the owner of the device (the 'user'), and shall show a form with two buttons (Yes and No).
The button press shall be sent to a central server (on the LAN).
The server shall collect and store the votes from the users, and shall choose when to enable the buttons (voting session opened), and when to disable them (voting session closed).
Trying to be clearer, the app shall replace current devices, which look like this one:
It's not so clear from the picture, but the device has two buttons, one for "Yes" vote, and one for "No" (there are more buttons, but their use is irrilevant, here).
Initially the app will handle only the vote casting (not the audio).
I'm totally new to web realtime programming, so I'm in doubt about which software stack of libraries to adopt for the project.
Googling around, I see socket.io could be a good solution, but I prefer asking the community, to try to find the most suited solution for the job.
So the final question: do you know of any open-source project, tutorial, bolg post, anything I could study to start designing the app I tried to describe?
Any solution, observation, opinion, real-life experience would be welcome, too...
I would also look at PubNub, which is a realtime global data stream network--here's a good voting app tutorial to get started: https://www.pubnub.com/blog/2014-11-18-power-a-realtime-voting-app-with-live-results-using-d3js/
There's great developer resources to get your started, for help, and lots of add-ons, like presence, history, and realtime charts, like EON: https://www.pubnub.com/developers/eon/
Socket.io is used essentially when you need something in realtime. The tradition RESTful APIs are a better/proven method.
To get started I would suggest reading up on the MEAN stack (also allows you to use the MongoDB database), A couple of tutorials should be a good starting point :
https://scotch.io/tutorials/setting-up-a-mean-stack-single-page-application
https://thinkster.io/mean-stack-tutorial
https://www.npmjs.com/package/slush-meanjs is a good resource to scaffold out your project. Slush is a generator that uses GULP as compared to yeoman generators which are only just beginning to implement gulp as its task-runner (currently uses grunt as the default task runner). Yeoman still doesnt have a great generator for the MEAN stack. If however you plan to exclude the database a good starting point would be to just use express on the backend and angularjs as the front end. To do this you can scaffold out a express project using generator-express (https://github.com/petecoop/generator-express) and once express is setup you can add angular to it using https://github.com/yeoman/generator-angular.
The directory would look something like this :
-app
---*client side libs*
-bin
--- www /* npm start to start the server */
-routes/ /* for all your express routing */
-test/
...
-app.js
...
For authentication you could use the scaffolded out CRUD model. If using socket.io for all the client server communication this is a good resource -
https://auth0.com/blog/2014/01/15/auth-with-socket-io/
I've been developing a web app using Node.js, Express, MongoDB, Mongoose and Socket.io for some time now. Version one has just been released and is working great. For version two however, I want to completely redesign the UI and use a front-end framework such as AngularJS. I've been looking at the (MEAN stack) and it looks really promising. Before goin into too much details, I will try to explain the purpose and working of the app so hopefully you will understand my question later on.
My app is a streaming service and has 4 simple models: user, room, song, playlist. These are modeled through Schema's in Mongoose and are stored in the MongoDB database. When a user visits the app, it serves all the HTML through Node.js and Express (and Jade as templating engine). If the user then registers, Socket.io lets the server know a user register, that is then saved to the database and the user is joined in the lobby room. In this room, users can chat, listen to music, etc. This is what I've been using Socket.io for, the communication between users themselves and my server (the server sometimes serves html too through Socket.io). Basically all the messages between Socket.io and the server are really small (chat messages, room objects, song objects, etc).
I'm quite happy with this approach, however, on the client side, the code is not really separated. There are currently two JS files serving the logic. One handles the Socket.io events and manipulates the dom. The other one is a common JS file that does a bit of everything. These two files are now about 5000 lines each. This worries me and I want to find a solution for this in v2.
So I've been looking at AngularJS in the MEAN stack and how to incorporate it in my app. My question is, will this solve my client side problems and can would you recommend taking the effort to do so. Also, how well will Socket.io work with AngularJS? Having both of them might be an overkill for what I'm doing. Do you know of any possible other framework that might be easier to integrate with my current environment? I know it's a bit of a subjective question but if you have any tips or do or don'ts, thanks for sharing! I really appreciate your feedback and if you need more information, I'd be happy to share it with you!
Jordy
I think that real example will be sufficient: https://github.com/btford/angular-socket-io-seed
I plan to write a rather large-scale web application using JavaScript. I believe I will be developing the webapp using CanJS to organize my client-side pieces.
I am strongly considering using Node.js for my server-side component, but I was wondering what the best way to set up Node to accept and handle REST requests. I did some Googling and came across something called Express. Any comments on this?
Any help/suggestions would be greatly appreciated.
restify is a node.js module built specifically to enable you to build correct REST web services.
http://mcavage.github.com/node-restify/
Express is more directed towards full web application (rendering HTML, etc). Restify is just for web services.
Besides, the guy who created Restify works at Joyent, the company where Ryan Dahl developped Node.js (which is kind of guarantee quality).
For REST request handling, express-resource is a more focused solution than railwayjs that's also built on top of expressjs.
You can try http://railwayjs.com/ it is build on top of http://expressjs.com/
Express is nice and fast :) railway has similar stack to ruby on rails.
http://expressjs.com is the de factor standard, does Sinatra-like routing. If you want real easy automatic generation of resources (sort of Rails-style), try one of those libraries above.
If you want it real simple on top of express, try http://github.com/deitch/booster
Disclosure: I just released Booster this week.