node.js workshop- node.js middleware

17
middleware for node Qiong Wu gfnork webentwicklungsunternehmen connect.js, express.js and other fun things

Upload: qiong-wu

Post on 10-May-2015

694 views

Category:

Software


3 download

TRANSCRIPT

Page 1: node.js workshop- node.js middleware

middleware for node

Qiong Wugfnork webentwicklungsunternehmen

connect.js, express.js and other fun things

Page 2: node.js workshop- node.js middleware

2

what is middleware?

node.js without middleware is very bare metalThe philosophy: implement as little as possible within node, allow community to build components on top of node - middleware

functions that handle requests and exposes them to other functionsforms a middle layer between request and application logichides unnecessary processing logic from application developers so they can focus on their application logic

Page 3: node.js workshop- node.js middleware

3

middleware for node.js

connect.js – a middleware framework, makes it easy to integrate middlewareexpress.js – view rendering and route management, not dependent on connect.js since Version 4.xpassport.js – authentication middleware

Page 4: node.js workshop- node.js middleware

4

connect.js

Middleware framework

var app = connect() .use(connect.logger('dev')) .use(connect.static('public')) .use(function (req, res) { res.end('hello world\n'); })

http.createServer(app).listen(3000);

Create connect

Use middleware

Process request

Create Server listening on Port 3000, pass

connect to requestListener

Page 5: node.js workshop- node.js middleware

5

connect.js

Authoring middleware for connect.js

Connect middleware is simply a function which accepts the request, response objects. Optionally the third parameter next can be used to continue down the middleware stack.

function SampleMiddleware(req, res, next) { next();}

Response object that is used to write data

back to the client

Request object containing the client

request

Pass control to the next middleware layer, optionally

supply Error

Page 6: node.js workshop- node.js middleware

6

connect.js

Usually you put your middleware in a separate module and export the function you have just authored

App.use from connect invoking module

Of course there is already plenty of choice when it comes to useful middleware, especially when it comes to standard stuff:Logger, compress, static, json, directory, favicon, etc.

Page 7: node.js workshop- node.js middleware

7

express.js

minimal and flexible node.js web application frameworkvery thin and performantperforms basic request routingapp.get for HTTP GETapp.post for HTTP POSTalso for other HTTP verbs

Plus View Rendering

No longer has connect.js as a dependency since Version 4.xThis tutorial is based on express.js 4.x, so be careful here

Page 8: node.js workshop- node.js middleware

8

express.js

Lets take a look again at the previous node.js example

response to a HTTP request can be sent, but e.g. information about HTTP verb has to be extracted from req object

var http = require('http');

http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n');}).listen(1337, '127.0.0.1');console.log('Server running at http://127.0.0.1:1337/');

Page 9: node.js workshop- node.js middleware

9

express.js

with express the code changes from

to

var http = require('http');

http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n');}).listen(1337, '127.0.0.1');console.log('Server running at http://127.0.0.1:1337/');

var express = require('express');var app = express();

app.get('/', function (req, res) { res.send('hello world');});

app.listen(3000);

Create express application

Import express module

Create route for GET / ROUTE

Page 10: node.js workshop- node.js middleware

10

express.js

Route management is very important because web applications usually respond to HTTP web requests

REST helps for structuring, basically means that URL defines a tree that defines the content

e.g. http://examplewebsite/customers/showallhttp://examplewebsite/customers/addhttp://examplewebsite/customers/delete

Add a route handler for each branch with express.js and your‘re fine!

Page 11: node.js workshop- node.js middleware

11

express.jsview renderingapart from route management, express.js can also handle view renderingView rendering takes care of dynamic server side generation for views that are transmitted and displayed on the client side

Careful though! views are only dynamic on the server, the client receives a static HTML+CSS view that is made dynamic by using CLIENT side JavaScript

View rendering is done with express.js and a rendering engine, e.g. Jade, Handlebars, DoT, lots of others

Page 12: node.js workshop- node.js middleware

12

express.jsview renderingRegister view rendering engine by invoking

Set default view rendering engine by invoking

View rendering engines have to comply to express.js callback structureCheck https://github.com/visionmedia/consolidate.js for further reading on the subject

app.engine('jade', require('jade').__express);

app.set('view engine', 'jade');

Express interface provided by rendering

engineThese have to match

These have to match

Page 13: node.js workshop- node.js middleware

13

express.jsview renderingAs soon as you want to render a front end view just call res.render

If you omit the callback, the rendered HTML is sent to the client with HTTP 200 response

res.render('email', { name: 'Tobi' }, function (err, html) { // ...});

View that is to be rendered

Pass Locals to View rendering engine

Callback that is invoked after view

is generated

The string that has been rendered

Page 14: node.js workshop- node.js middleware

14

express.jsview renderingLets take Jade as an exampleJade Syntax Locals

Page 15: node.js workshop- node.js middleware

15

express.jsview renderingLets take Jade as an exampleJade Syntax Locals HTML output

Page 16: node.js workshop- node.js middleware

16

passport.js

authentication middleware, depends a little on database communication basics, we’ll talk about that tomorrow

Page 17: node.js workshop- node.js middleware

17

How should I structure my application?

Web development patterns provide the answer for that, MVC is one example, we’ll also talk about that tomorrow