Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NodeJS Middleware

Yashprit
August 29, 2014

NodeJS Middleware

Understanding middleware concept. Exploring existing middleware like connect and express

Yashprit

August 29, 2014
Tweet

More Decks by Yashprit

Other Decks in Programming

Transcript

  1. Agenda • Middleware Concept • Connect Middleware with Example •

    Express Middleware. • Express Philosophy. • Express Cool Features.
  2. What is Middleware in NodeJS? The middleware is called as

    a chain of functions, with order based on middleware definition order(time) with matching routes (if applicable). e.g. req and res objects are travelling through chain so you can reuse/improve/modify data in them along the chain. /* This is only for demonstration purpose*/ function(req, res, next) { req.name = “samething”; next(/* err and/or output */) }
  3. Middleware cont. • This is sometime also called as Pipelines.

    Pipelines are everywhere, under various forms, uses and purposes. • Generically, a Pipeline is a series of processing units connected together, where the output of one unit is the input for the next one. (In Node.js, this often means a series of functions in the form.) • If your middleware should return a response, just do so. If it shouldn't, it's implied that some later middleware should return a response. • If you need to pass along data to that later part of the process, you should attach it to an appropriate part of the request object req. P.S. Don’t confuse here about pipe(), thats belongs to another hero streams, streams are better suited to process flowing data
  4. Middleware Cont. Two general use cases for middleware :- •

    Generic: It will apply to every single request. Each middleware have to call next() inside, if it wants to proceed to next middleware. • Specific: When you use app.get('/path', function(...) this actual function is middleware as well, just inline defined The chain order is based on definition order. So it is important to define middleware in sync manner or order-reliable async manner. Otherwise different order of middleware can break logic.
  5. Middleware : Connect • Node.js itself offers an HTTP module,

    whose createServer method returns an object that you can use to respond to HTTP requests. • That object inherits the http.Server prototype. From the README: Connect is an extensible HTTP server framework for node, providing high performance "plugins" known as middleware. More specifically, connect wraps the Server, ServerRequest, and ServerResponse objects of node.js' standard httpmodule, giving them a few nice extra features, one of which is allowing the Server object to use a stack of middleware.
  6. List of common uses connect middlewares • logger request logger

    with custom format support • csrf Cross-site request forgery protection • compress Gzip compression middleware • basicAuth basic http authentication • bodyParser extensible request body parser • json application/json parser • multipart multipart/form-data parser • timeout request timeouts • cookieParser cookie parser • session session management support with bundled MemoryStore • cookieSession cookie-based session support • methodOverride faux HTTP method support • favicon efficient favicon server (with default icon) • query automatic querystring parser, populating req. query • errorHandler flexible error handler Connect has 18 bundled middleware and a rich selection of 3rd-party middleware.. That's why Connect describes itself as a "middleware framework," and is often analogized to Ruby's Rack.
  7. Adding middleware stack for server To add a middleware to

    the stack of middleware for a server, we use it like so: connect = require('connect') stephen = connect.createServer() stephen.use(worseThanUselessMiddleware) Finally, you can also specify a path prefix when adding a middleware, and the middleware will only be asked to handle requests that match the prefix: connect = require('connect') bob = connect.createServer() bob.use('/attention', worseThanUselessMiddleware)
  8. Creating Simple Middleware function uselessMiddleware(req, res, next) { next() }

    A middleware can also signal an error by passing it as the first argument to next: // A middleware that simply interrupts every request function worseThanUselessMiddleware(req, res, next) { next("Hey are you busy?") } When a middleware returns an error like this, all subsequent middleware will be skipped until connect can find an error handler.
  9. URL based authentication policy function authenticateUrls(urls /* basicAuth args*/) {

    basicAuthArgs = Array.slice.call(arguments, 1) basicAuth = require('connect').basicAuth.apply(basicAuthArgs) function authenticate(req, res, next) { // Warning! assumes that urls is amenable to iteration for (var pattern in urls) { if (req.path.match(pattern)) { basicAuth(req, res, next) return } } next() } return authenticate }
  10. Role based authorization function authorizeUrls(urls, roles) { function authorize(req, res,

    next) { for (var pattern in urls) { if (req.path.match(pattern)) { for (var role in urls[pattern]) { if (users[req.remoteUser].indexOf(role) < 0) { next(new Error("unauthorized")) return } } } } next() } return authorize }
  11. Error handling email = require('node_mailer') function emailErrorNotifier(generic_opts, escalate) { function

    notifyViaEmail(err, req, res, next) { if (err) { var opts = { subject: "ERROR: " + err.constructor.name, body: err.stack, } opts.__proto__ = generic_opts email.send(opts, escalate) } next() } }
  12. Putting it all together private_urls = { '^/attention': ['coworker', 'girlfriend'],

    '^/bank_balance': ['me'], } roles = { stephen: ['me'], erin: ['girlfriend'], judd: ['coworker'], bob: ['coworker'], } passwords = { me: 'doofus', erin: 'greatest', judd: 'daboss', bob: 'anachronistic discomBOBulation', } function authCallback(name, password) { return passwords[name] === password } stephen = require('connect').createServer() stephen.use(authenticateUrls(private_urls, authCallback)) stephen.use(authorizeUrls(private_urls, roles)) stephen.use('/attention', worseThanUselessMiddleware) stephen.use(emailErrorNotifier({to: '[email protected]'})) stephen.use('/bank_balance', function (req, res, next) { res.end("Don't be Seb-ish") }) stephen.use('/', function (req, res, next) { res.end("I'm out of coffee") })
  13. Middleware: Express Express does to Connect what Connect does to

    the http module: It offers a createServer method that extends Connect's Server prototype. So all of the functionality of Connect is there,plus view rendering and a handy DSL for describing routes. Ruby's Sinatra is a good analogy.
  14. Philosophy behind ExpressJS • This is probably one of the

    most popular frameworks in the Node.js community of coders right now. The core philosophy behind ExpressJS is to offer an application framework for single or multi-page web apps, using views and template engine. • The Express.js framework also includes a multitude of utilities to work with HTTP and building APIs. The pluggable middleware in the framework provides a great way to add elements, such as passportjs, as you need them.
  15. Cool feature 1: routing var express = require("express"); var http

    = require("http"); var app = express(); app.all("*", function(request, response, next) { response.writeHead(200, { "Content-Type": "text/plain" }); next(); }); app.get("/", function(request, response) { response.end("Welcome to the homepage!"); }); app.get("/about", function(request, response) { response.end("Welcome to the about page!"); }); app.get("*", function(request, response) { response.end("404!"); }); http.createServer(app).listen(1337); Routing is a way to map different requests to specific handlers.
  16. Cool feature 2: request handling Express augments the request and

    response objects that you're passed in every request handler. response.redirect("/hello/anime"); response.redirect("http://www.myanimelist.net"); response.redirect(301, "http://www.anime.org"); // HTTP status code 301 This isn't in vanilla Node and it's also absent from Connect, but Express adds this stuff. It adds things like sendFile which lets you just send a whole file: response.sendFile("/path/to/anime.mp4"); The request gets a number of cool properties, like request.ip to get the IP address and request.files to get uploaded files.
  17. Cool feature 3: views More features? Oh, Express, I'm blushing.

    Express can handle views. It's not too bad. Here's what the setup looks like: // Start Express var express = require("express"); var app = express(); // Set the view directory to /views app.set("views", __dirname + "/views"); // Let's use the Jade templating language app.set("view engine", "jade"); The first block is the same as always. Then we say "our views are in a folder called 'views'".
  18. Bonus cool feature: everything from Connect and Node I want

    to remind you that Express is built on top of Connect which is built on top of Node. This means that all Connect middleware works with Express. This is useful! For example: var express = require("express"); var app = express(); app.use(express.logger()); // Inherited from Connect app.get("/", function(req, res) { res.send("anime"); }); app.listen(1337);