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

Node.js - from the beginning

Node.js - from the beginning

Robert Prediger

March 31, 2016
Tweet

More Decks by Robert Prediger

Other Decks in Programming

Transcript

  1. Page 1 Node.js from the beginning LINKWORK, März 2016, Landshut

    22-Apr-16 Node.js – from the Beginning
  2. Page 2 §  Robert Prediger §  20 years: Progress § 

    15 years: Web development §  5 years: Node.js §  [email protected] 22-Apr-16 Introduction Node.js – from the Beginning
  3. Page 5 npm is a piece of technology, but more

    importantly, it is a community. npm makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that you're sharing. https://www.npmjs.com/ Node.js – from the Beginning npm 22-Apr-16
  4. Page 8 Socket.IO enables real-time bidirectional event-based communication. It works

    on every platform, browser or device, focusing equally on reliability and speed. USED BY EVERYONE From Microsoft Office, Yammer, Zendesk, Trello... to hackathon winners and little startups. http://socket.io/ Node.js – from the Beginning socket.io 22-Apr-16
  5. Page 9 var express = require('express'), app = express(), server

    = require('http').createServer( app ).listen( config.port ), io = require('socket.io').listen( server ), numConn = 0; ... Node.js – from the Beginning socket.io 22-Apr-16
  6. Page 10 ... io.on( 'connection', function(socket) { numConn += 1;

    console.log( "connected", numConn ); // event for login from socket socket.on( "login", function ( name ) { socket.nickname = name; }); ... socket.on( "disconnect", function() { numConn -= 1; console.log( "disconnect", numConn ); }); }); Node.js – from the Beginning socket.io 22-Apr-16
  7. Page 11 ... // socket sends message socket.on( "sendMessage", function(

    msg ) { console.log( "send message", numConn, socket.nickname, msg ); // broadcast message to all subscribed sockets socket.broadcast.emit( "newMessage", { name: socket.nickname, msg: msg } ); }); ... Node.js – from the Beginning socket.io 22-Apr-16
  8. Page 12 Fast, unopinionated, minimalist web framework for Node.js Minimal

    and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. http://expressjs.com/ Node.js – from the Beginning express 22-Apr-16
  9. Page 13 express 22-Apr-16 Node.js – from the Beginning favicon

    Static-Files Log Process Request ... (other middleware modules)
  10. Page 14 var express = require('express'), app = express(); app.get(

    "/rest/:table", auth, function( req, res ) { res.send( "Request: " + req.params.table ); }); app.get( "/rest/:table/:id", auth, function( req, res ) { res.send( req.params ); }); app.get( "/", function( req, res ) { console.log( "req" ); res.send( "Hello World" ); }); app.listen( 80 ); Node.js – from the Beginning express 22-Apr-16
  11. Page 15 var express = require('express'), app = express() ,

    router = express.Router(); // use router with prefix app.use( "/rest", auth, router ); router.get( "/:table", function( req, res ) { res.send( "Request: " + req.params.table ); }); router.get( "/:table/:id", function( req, res ) { res.send( req.params ); }); app.get( "/", function( req, res ) { console.log( "req" ); res.send( "Hello World" ); }); app.listen( 80 ); Node.js – from the Beginning express 22-Apr-16
  12. Page 16 var express = require('express'), app = express(); app.use(

    function( req, res, next ) { var start = new Date; console.log( "way down", req.url ); next(); console.log( 'way back' ); console.log( 'Response-Time', new Date - start ); }); app.get( "/", function( req, res ) { console.log( "req" ); res.send( "Hello World" ); }); app.listen( 80 ); Node.js – from the Beginning express 22-Apr-16
  13. Page 17 var express = require('express'), app = express(); app.use(

    function( req, res, next ) { var start = new Date; console.log( "way down", req.url ); next(); console.log( 'way back' ); console.log( 'Response-Time', new Date - start ); }); app.get( "/", function( req, res ) { process.nextTick( function() { console.log( "req" ); res.send( "Hello World" ); }); }); app.listen( 80 ); Node.js – from the Beginning express 22-Apr-16
  14. Page 18 Enterprise Node to Power the API Economy No

    matter where you are in the lifecycle of developing APIs with Node, StrongLoop has a complete set of command-line and graphical tools that can work together or independently to help you succeed. https://strongloop.com/ Node.js – from the Beginning StrongLoop 22-Apr-16
  15. Page 22 The web framework of your dreams Built for

    developers by a giant squid. Sails makes it easy to build custom, enterprise-grade Node.js apps. It is designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps. http://sailsjs.org/ Node.js – from the Beginning SailsJS 22-Apr-16
  16. Page 23 SailsJS Node.js – from the Beginning 22-Apr-16 Source:

    https://www.manning.com/books/sails-js-in-action
  17. Page 24 SailsJS Node.js – from the Beginning 22-Apr-16 Source:

    https://www.manning.com/books/sails-js-in-action
  18. Page 25 next generation web framework for node.js Koa is

    a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. http://koajs.com/ Node.js – from the Beginning Koa 22-Apr-16
  19. Page 26 var koa = require('koa'), app = koa(); app.use(

    function *(next){ var start = new Date; yield next; var ms = new Date - start; this.set('Response-Time', ms + 'ms'); }); app.use( function *(){ this.body = 'Hello World'; }); app.listen( 80 ); Node.js – from the Beginning Koa (1.x) 22-Apr-16
  20. Page 27 var Koa = require('koa'), app = new Koa(),

    co = require('co'); app.use( co.wrap( function *( ctx, next){ const start = new Date; yield next(); var ms = new Date - start; console.log( 'Response-Time', ms ); })); app.use( ctx => { ctx.body = 'Hello World'; }); app.listen( 80 ); Node.js – from the Beginning Koa (2.x) 22-Apr-16
  21. Page 28 The Web framework beyond your dreams ThinkJS is

    the first Node.js MVC framework that supporting use full ES6/7 features to develop Node.js application. https://thinkjs.org/ Node.js – from the Beginning ThinkJS 22-Apr-16
  22. Page 32 Disadvantages Even though ThinkJS has many advantages, it

    has also a few disadvantages too, for example: •  ThinkJS is a relatively new framework, the community is not strong enough. •  ThinkJS is short of large scale applications. Node.js – from the Beginning ThinkJS 22-Apr-16
  23. Page 33 Build powerful back-end with no effort. Fast. Reusable.

    Easy to use. The next generation framework for Node.js built on top of Koa. http://strapi.io/ Node.js – from the Beginning strapi 22-Apr-16
  24. Page 37 22-Apr-16 Node.js – from the Beginning That‘s it

    J Thank you very much for listening [email protected] Sourcecode:https://github.com/web4biz/Node.js-FromTheBeginning