express yourself

37
Node.js Express Yourself: Building Web Applications with Node.js and Express Yaniv Rodenski - @YRodenski

Upload: yaniv-rodenski

Post on 06-May-2015

1.551 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Express yourself

Node.js

Express Yourself: Building Web Applications with Node.js and Express

Yaniv Rodenski - @YRodenski

Page 2: Express yourself

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

Page 3: Express yourself

About 1997:

Page 4: Express yourself

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

Page 5: Express yourself

Agenda

• What is Node.js• The Importance of Being Asynchronous• NPM• Connect Middleware• Express

Page 6: Express yourself

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:

Page 7: Express yourself

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

Page 8: Express yourself

Demo

Hello Node.js

Page 9: Express yourself

Synchronous server operations// GET api/countriespublic string Get(){

var client = WebRequest.Create("http://.../");var response = client.GetResponse();var stream = response.GetResponseStream();var reader = new StreamReader(stream);return reader.ReadToEnd();

}

Page 10: Express yourself

Synchronous Server Operations

Client

DBServer

Client

Page 11: Express yourself

Same-Same but Different// GET api/countriesPublic async Task<string> Get(){

var client = new HttpClient();var response = await

client.GetAsync("http://...");return await

response.Content.ReadAsStringAsync();}

Page 12: Express yourself

Asynchronous Server Operations

Client

DBServer

Client

Page 13: Express yourself

Demo

Async server in Node.js

Page 14: Express yourself

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

Page 15: Express yourself

Node Package Manager (NPM)

• The Node Package Manager (NPM) provides a management mechanism for modules• Download and install• Version management• Deployment

Page 16: Express yourself

Demo

Getting Express.js

Page 17: Express yourself

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 codenext();

})

Page 18: Express yourself

Demo

Simple HTTP server using connect

Page 19: Express yourself

Out-of-the-Box Middleware Components

• Logging• Body parser (JSON/forms)• Cookies• Error handling • Session management• Basic authentication

Page 20: Express yourself

Demo

Using Cookies and Sessions

Page 21: Express yourself

ExpressJS

• ExpressJS is a web application framework inspired by Ruby’s Sinatra

• Provides a model-view-route architecture

Page 22: Express yourself

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){});

Page 23: Express yourself

Demo

Simple routing

Page 24: Express yourself

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

Page 25: Express yourself

Demo

Passing Parameters to Routes

Page 26: Express yourself

Configuring Express

• Express provides the configure method to perform configuration:• Setting up Connect middleware• Setting up application level variables using

app.set

Page 27: Express yourself

Views

• Views are a template-based UI mechanism• Express supports many view-engines

including:• Jade• JSHtml• EJS

Page 28: Express yourself

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

Page 29: Express yourself

Demo

Basic Jade Template

Page 30: Express yourself

Mixing Up JavaScript & Jade

• Jade fully supports JavaScript using the script element:• Linking to external JS files• Embedding in-line JavaScript

Page 31: Express yourself

Demo

Jade and bootstrap

Page 32: Express yourself

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

Page 33: Express yourself

Demo

Using Blocks

Page 34: Express yourself

ModelModel

• Express provides a mechanism to insert data-models into views

• The rendering of the complete artifact is up to the view engine

Page 35: Express yourself

Demo

Rendering Data with Jade

Page 36: Express yourself

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

Page 37: Express yourself

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