Slide 1

Slide 1 text

JavaScript beyond the browser Henrique Vicente http://henvic.github.io/

Slide 2

Slide 2 text

JavaScript • Created by Brendan Eich • First public appearance on the betas of Netscape Navigator 2.0

Slide 3

Slide 3 text

ECMAScript • “JS standardization / formalization” • ECMA-262, ISO/IEC 16262 • Gave origin to [other] new implementations such as ActionScript, QML, QtScript, …

Slide 4

Slide 4 text

Why JS on the browser? • How else would you offer a better experience for applications such as web mail? • More portable • Everywhere • Fast (and getting faster) • Simplicity

Slide 5

Slide 5 text

Alternatives on the browser • Less performance • No broad support • Not as simple • ???

Slide 6

Slide 6 text

And why JavaScript beyond the browser? 1. Code re-use / avoid duplicity 2. Using your JS know-how to attack even more problems 3. JavaScript is ubiquitous and everybody uses it 4. Why not?

Slide 7

Slide 7 text

Web application with JS both on the client-side as well as on the server-side • Share data validations code:
 
 Less inconsistencies
 
 Just-in-time feedback to the end-user
 
 Still have safe verification on the server-side * * Thou shalt never trust the user https://www.owasp.org/index.php/Don't_trust_user_input

Slide 8

Slide 8 text

JS runtimes • Google V8 (Chrome) • SpiderMonkey (Firefox) • WebKit (Safari) • Trident (IE) • Rhino • Wakanda • …

Slide 9

Slide 9 text

JS beyond the browser • Web apps for Chrome, Firefox * • Apache Cordova / PhoneGap • PhantomJS • SlimerJS • CasperJS • MongoDB (the map / reduce functions) • Node.js * as extensions / app, instead of common web sites

Slide 10

Slide 10 text

Web apps for Chrome, Firefox • On the browser • But with access to APIs restricted to regular web pages • Apps installed as extensions, not just regular web pages

Slide 11

Slide 11 text

• Mobile framework for developing using a web stack (HTML, JS, CSS) • Lets you develop a single system for different mobile platforms (i.e., Android, iOS, Windows Phone…)

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

• JavaScript programmable web stacks • PhantomJS: headless, WebKit-based • SlimerJS: Gecko-based • Use cases:
 web crawler, functional tests, monitoring, bots

Slide 14

Slide 14 text

Examples • Scripts from my“Tests and automation with PhantomJS and CasperJS” talk
 
 https://speakerdeck.com/henvic/tests-and- automation-with-phantomjs-and-casperjS
 
 https://github.com/henvic/phantom-casper- simple-talk

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Example: very simple functional tests with Wordpress e CasperJS https://github.com/henvic/phantom-casper-simple-talk/blob/master/wordpress.js

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Node.js • Great community, growing fast • Excellent for CLI tools, prototyping, and… • Taking some room from Apache + PHP, Python, Ruby, etc, as a serious web application stack • non-blocking I/O, single-threaded event loop
 this can make your brain blow if you’re not used to concurrency… (event-driven programming)

Slide 19

Slide 19 text

npm ~ Node’s apt-get

Slide 20

Slide 20 text

npmjs.org • Anyone can publish a npm package by just preparing it and invoking “npm publish” • Many great package available • Be aware of what code you run due to security

Slide 21

Slide 21 text

npm ~ Node’s apt-get • npm_modules - npm modules directory • There are local and global modules • Each module has its own dependencies (other modules) installed inside their own npm_modules

Slide 22

Slide 22 text

Adding a module to your program • var expressValidator = require(‘express-validator’);
 // Includes the express-validator module, available on npm
 
 Install it locally with
 npm install express-validator
 
 You shall install it locally.

Slide 23

Slide 23 text

What should be installed globally? • Tools like Cordova, grunt-cli, bower, services… • npm install -g or
 npm install -g

Slide 24

Slide 24 text

Real world example: Creating a chat prototype for a corporate social network I used socket.io for this, but would try Faye with its Bayeux protocol if I knew it by the time I built it.

Slide 25

Slide 25 text

Chat final implementation running on production, also with Node.js & Socket.IO

Slide 26

Slide 26 text

Serving static files 1. Node.JS doesn’t provide an official way to do this. 2. Install a module that does so, like node-static
 > npm install node-static
 
 then, write a simple program to do the job:

Slide 27

Slide 27 text

Compute n! with Node.JS

Slide 28

Slide 28 text

package.json • To publish a Node package you need to write this document • Even if you don’t intend to publish a npm package, create a package.json document to take advantage of a series of things like dependency management, etc

Slide 29

Slide 29 text

package.json

Slide 30

Slide 30 text

Some fantastic tools built with Node.js

Slide 31

Slide 31 text

Slides available on https://speakerdeck.com/henvic/javascript-beyond-the- browser