Complete MVC on NodeJS

14,357 views

Published on

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

Published in: Technology
2 Comments
34 Likes
Statistics
Notes
No Downloads
Views
Total views
14,357
On SlideShare
0
From Embeds
0
Number of Embeds
2,356
Actions
Shares
0
Downloads
281
Comments
2
Likes
34
Embeds 0
No embeds

No notes for slide

Complete MVC on NodeJS

  1. 1. Complete MVC with NodeJS Hüseyin BABAL Software Developer, CSM @Sony Eurasia
  2. 2. Who Am I Hüseyin BABAL Software Developer, CSM @Sony Eurasia Open Source Comitter Linux, JAVA, PHP, MongoDB, Javascript, NodeJS, Elasticsearch are my job  GDG event speaker
  3. 3. WHAT IS NODE.JS?
  4. 4. NodeJS is a software platform that is used to build scalable network applications
  5. 5. ● Author: Ryan Lienhart Dahl ● Built on Chrome V8 Engine ● Written in C++, Javascript ● Sponsored by Joyent
  6. 6. SO, WHAT IS NODE.JS?
  7. 7. V8 Engine JS on Server Side Single Thread Event Loop Non-Blocking I/O
  8. 8. WHY TO USE NODE.JS?
  9. 9. I/O is Expensive
  10. 10. Apache:Memory usage increases per connection Nginx:Constant memory usage per connection, because nginx uses event loop mechanism
  11. 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. 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. 13. How about this answer? Single thread with non-blocking I/O
  14. 14. WHAT IS EVENT LOOP AND NON-BLOCKING I/O?
  15. 15. Blocking I/O Busy process Block process until job is done Process available Non-Blocking I/O Event Stack cb Do the job and notify program when it’s done
  16. 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. 17. SEEMS COOL, HOW ABOUT CODING?
  18. 18. You know JS? Then you code NPM (Node Package Manager) Available Windows/Linux/MAC Webstorm is the best, also use notepad++
  19. 19. npm install “module_name” Import module with “require” File : server.js Simple web server!!! node server.js
  20. 20. To test web server, go to url http://127.0.0.1:1337 What about if you try to make a system have such url: http://127.0.0.1:1337/product/1234.html ???
  21. 21. ANSWER:EXPRESS.JS
  22. 22. Sinatra inspired web development framework for node.js
  23. 23. HOWTO START?
  24. 24. You have already installed node.js before… npm install express -g That’s it! You can use express framework on your project
  25. 25. Ideal project structure for nodejs based web applications
  26. 26. Model {mongoose} View {jade} Controller {express built-in fns}
  27. 27. A quick scenario… ● User goes http://127.0.0.1:1337/tickets/devfestticket54332.html ● System fetchs ticket information from db by using id(54332) ● Assign ticket information to view ● Render view and send to user browser
  28. 28. MODEL DESIGNING
  29. 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 use Assume this model defined in a file called “Ticket.js”
  30. 30. CONTROLLER
  31. 31. *Controller isthe behaviour of your program *Put your controller definition files under “controller” folder.
  32. 32. VIEW
  33. 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. 34. Indendation based template engine
  35. 35. COMBINE ALL
  36. 36. Controllers, Views, Models managed by main bootstrap file called “app.js” 1 2 Go to project folder and say 3 node app.js 4 5
  37. 37. BEST PRACTICES
  38. 38. WHO USES NODEJS?
  39. 39. Node.js https://github.com/joyent/node express.js https://github.com/visionmedia/express mongoose https://github.com/LearnBoost/mongoose jade https://github.com/visionmedia/jade Express Simple Blog https://github.com/nodejstr/expressSimpleBlog NodeJS Türkiye http://www.nodejstr.com/
  40. 40. Questions?
  41. 41. Thank you 

×