Express yourself

1,906 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,906
On SlideShare
0
From Embeds
0
Number of Embeds
347
Actions
Shares
0
Downloads
22
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • Explain the basic node concepts:Modules and how we import them using requireThe use of callbacks
  • Explain the basic node concepts:Modules and how we import them using requireThe use of callbacks
  • Express yourself

    1. 1. Node.js Express Yourself: Building Web Applications with Node.js and Express Yaniv Rodenski - @YRodenski
    2. 2. About me: • Senior Architect @ Sela • Windows Azure MVP • Co-manager of the Windows Azure Community • Co-author of Developing Windows Azure and Web Services (MOC 20487) • Developing software professionally since 1997
    3. 3. About 1997:
    4. 4. About 1997: Script-based server side Shared hosting environment Browser wars New HTML standard that will “Change the World” This guy was the PM of Israel
    5. 5. Agenda • What is Node.js • The Importance of Being Asynchronous • NPM • Connect Middleware • Express
    6. 6. What is Node.js • A JavaScript runtime that is designed for asynchronous IO operations • Very lightweight and fast • In use by a growing number of companies:
    7. 7. The Node.js Ecosystem • Node.js has a rapidly growing ecosystem: – Web frameworks: • Express.js • Socket.io – Database support: • MongoDB • SQL Server – Hosting and Cloud environments: • IIS, Azure • Forever.js • Joyent, Heroku
    8. 8. Demo Hello Node.js
    9. 9. Synchronous server operations // GET api/countries public string Get() { var client = WebRequest.Create("http://.../"); var response = client.GetResponse(); var stream = response.GetResponseStream(); var reader = new StreamReader(stream); return reader.ReadToEnd(); }
    10. 10. Synchronous Server Operations Client DBServer Client
    11. 11. Same-Same but Different // GET api/countries Public async Task<string> Get() { var client = new HttpClient(); var response = await client.GetAsync("http://..."); return await response.Content.ReadAsStringAsync(); }
    12. 12. Asynchronous Server Operations Client DBServer Client
    13. 13. Demo Async server in Node.js
    14. 14. Async JavaScript with Node.js • Node.js is asynchronous by design • Most IO operations have no synchronous API • This is crucial since Node.js (or rather V8) has a single thread
    15. 15. Node Package Manager (NPM) • The Node Package Manager (NPM) provides a management mechanism for modules • Download and install • Version management • Deployment
    16. 16. Demo Getting Express.js
    17. 17. Connect Middleware • Node.js http module provides bare-bones HTTP server functionality • Connect middleware (by Sencha Labs) provides an expandable pipeline on top of Node.js's httpServer • You can add any functionality to the pipeline by calling the use method, and passing a method: server.use(function(req, res, next){ // some code next(); })
    18. 18. Demo Simple HTTP server using connect
    19. 19. Out-of-the-Box Middleware Components • Logging • Body parser (JSON/forms) • Cookies • Error handling • Session management • Basic authentication
    20. 20. Demo Using Cookies and Sessions
    21. 21. ExpressJS • ExpressJS is a web application framework inspired by Ruby’s Sinatra • Provides a model-view-route architecture
    22. 22. Routing • Routing is one of the pillars of ExpressJS • To create a route use the app.verb convention: app.get('route',function(req,res){ }); app.post('route',function(req,res){ }); app.all('route',function(req,res){ });
    23. 23. Demo Simple routing
    24. 24. Routing and Parameters • Express supports parameters as part of the URI • Declare a parameter in the URI using a placeholder • Access query-string parameters using req.query • Use regular expression
    25. 25. Demo Passing Parameters to Routes
    26. 26. Configuring Express • Express provides the configure method to perform configuration: • Setting up Connect middleware • Setting up application level variables using app.set
    27. 27. Views • Views are a template-based UI mechanism • Express supports many view-engines including: • Jade • JSHtml • EJS
    28. 28. Jade • Jade is Express’s default view engine • It is based on Haml in order to provide a clean syntax for generating HTML • Tab based • Full support for HTML
    29. 29. Demo Basic Jade Template
    30. 30. Mixing Up JavaScript & Jade • Jade fully supports JavaScript using the script element: • Linking to external JS files • Embedding in-line JavaScript
    31. 31. Demo Jade and bootstrap
    32. 32. Blocks • In most applications, a number of UI components are used throughout the application • Blocks provide a way to declare a common layout that is shared among views
    33. 33. Demo Using Blocks
    34. 34. ModelModel • Express provides a mechanism to insert data- models into views • The rendering of the complete artifact is up to the view engine
    35. 35. Demo Rendering Data with Jade
    36. 36. Summary • Node.js allows JavaScript to run outside of browsers and perform IO • Asynchronous by nature • Lightweight, fast and extremely cool • Rich ecosystem for building JavaScript-based servers
    37. 37. What’s next • ExpressJS can be used to use the MVR/MVC patterns in web applications • Currently there are 1688 projects in NPM which use Express: Sails, Express.io, etc. • All you need is a text editor and a command line

    ×