Complete MVC on NodeJS

Download Complete MVC on NodeJS

Post on 06-May-2015




1 download


Complete MVC on NodeJS by using ExpressJS, Mongoose, Jade with best practises


  • 1.Complete MVC with NodeJSHseyin BABAL Software Developer, CSM @Sony Eurasia

2. Who Am I Hseyin BABAL Software Developer, CSM @Sony EurasiaOpen Source Comitter Linux, JAVA, PHP, MongoDB, Javascript, NodeJS, Elasticsearch are my job GDG event speaker 3. WHAT IS NODE.JS? 4. NodeJS is a software platform that is used to build scalable network applications 5. Author: Ryan Lienhart DahlBuilt on Chrome V8 EngineWritten in C++, JavascriptSponsored by Joyent 6. SO, WHAT IS NODE.JS? 7. V8 Engine JS on Server Side Single Thread Event LoopNon-Blocking I/O 8. WHY TO USE NODE.JS? 9. I/O is Expensive 10. Apache:Memory usage increases per connection Nginx:Constant memory usage per connection, because nginx uses event loop mechanism 11. Dealing with I/O Problems Synchronous - One request at a time Fork - New process for each request Thread - New thread for each request 12. Thread per request is memory expensive!What if all threads are busy? Multithread code is; difficult to debug difficult to write in efficiently performed 13. How about this answer?Single thread with non-blocking I/O 14. WHAT IS EVENT LOOP AND NON-BLOCKING I/O? 15. Blocking I/O Busy processBlock process until job is doneProcess availableNon-Blocking I/OEvent Stackcb Do the job and notify program when its done 16. Assign jobs to event stack, you will be notified One single thread can handle entire program Traditional threaded architecture is not an easy thing More thread means more process, equals more memory Min cost, speed development Scalable architecture Callback functions 17. SEEMS COOL, HOW ABOUT CODING? 18. You know JS? Then you code NPM (Node Package Manager)Available Windows/Linux/MAC Webstorm is the best, also use notepad++ 19. npm install module_nameImport module with requireFile : server.js Simple web server!!!node server.js 20. To test web server, go to url What about if you try to make a system have such url: ??? 21. ANSWER:EXPRESS.JS 22. Sinatra inspired web development framework for node.js 23. HOWTO START? 24. You have already installed node.js before npm install express -g Thats it! You can use express framework on your project 25. Ideal project structure for nodejs based web applications 26. Model {mongoose} View {jade}Controller {express built-in fns} 27. A quick scenario User goes System fetchs ticket information from db by using id(54332) Assign ticket information to view Render view and send to user browser 28. MODEL DESIGNING 29. Ticket = new Schema({ tId:{ type:String, required:true }, title:{ type:String, required:true }, organizationDate:{ type:Date, required:true }, owner:{ type:String, validate:[validator({ length:{ min:3, max:20 } }), "username"], required:false, default:"anonymous" } });Yes, model definition in json format. Actually, all the data in nodejs world are in json format.Model definition is done. This model should be exists under models folder. Require this model when you need to useAssume this model defined in a file called Ticket.js 30. CONTROLLER 31. *Controller isthe behaviour of your program *Put your controller definition files under controller folder. 32. VIEW 33. View files are under views folder. You can use template engines with express.js, in this scenario, jade used Create ticket-detail.jade, put it under views folder 34. Indendation based template engine 35. COMBINE ALL 36. Controllers, Views, Models managed by main bootstrap file called app.js12Go to project folder and say 3node app.js 45 37. BEST PRACTICES 38. WHO USES NODEJS? 39. Node.js Simple Blog Trkiye 40. Questions? 41. Thank you