JavaScript on the Command Line & Server @prpatel TripLingo Labs PRATIK@mypatelspace.com
Topics Modern JavaScript Why? Ecosystem Node Grunt
Yesterday s JavaScript
Today s JavaScript is cool
What s changed?
No Change JavaScript is essentially the same language as it was when invented 20 years ago
We ve changed JavaScript developers today understand: - Best Practices - Tooling - JavaScript Runtimes today are awesome
What hasn t changed JavaScript still has bad parts Async, event-driven programming model
How it run outside the browser?
V8 Super duper fast JavaScript runtime from Google Runs in the browser
Node.js uses V8
What about the JVM? Built into the JVM for years Rhino (current) Nashorn (next gen)
Why JavaScript on the server?
Why JS Single language for the browser & server Fast Scalable
ISOMORPHIC APPS
Isomorphic Apps Code can run either on the browser or server
Traditional Webapp
Isomorphic
Why Isomorphic? Flexibility Performance
Frameworks
Ecosystem
JavaScript Ecosystem Node NPM - node packaged modules Express - web framework Grunt - task runner / automation
Node.js JavaScript runtime based on V8
NPM Module / Package manager for node
Express Basic server-side web framework for Node
Grunt Task runner Automation build scripts
Node.js
Node.js Install from nodej.org Install using brew on Mac OSX
Node.js Command-line centric
Running Node Script $ node test3.js Hello Bangalore - test3.js: console.log('hello Bangalore')
Package.json // describes this node project, place in base folder { "name": "hello-world", "description": "hello world test app", "version": "0.0.1", "private": true, "dependencies": { "express": "3.x" } }
NPM
NPM Installed with Node.js https://www.npmjs.org/ Basic package management
Running NPM $ npm ls $ npm install lodash -g
Express
Express Very basic server-side Web Framework Lots of other options: - Sails.js - CompoundJS - Geddy
Express Implements event-loop model that is basis of Nodejs Async Event-driven Super-scalable
Express App $ cat hello-world/package.json { "name": "hello-world", "description": "hello world test app", "version": "0.0.1", "private": true, "dependencies": { "express": "3.x" } } $ npm install $ npm app Express server listening on port 3000
Quick Express Code Walkthrough
Grunt
Grunt Task based command-line build tool
What uses? Unit test running JavaScript linting Minification (both for browser and node.js) CI
package.json "devdependencies": { "grunt": "~0.4.2", "grunt-contrib-jshint": "~0.6.3", "grunt-contrib-nodeunit": "~0.2.0", "grunt-contrib-uglify": "~0.2.2" }
Gruntfile module.exports = function (grunt) { grunt.initconfig({ jshint: { all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js', 'public/**/*.js', 'routes/*.js', 'routes/**/*.js', 'app.js'] } }); grunt.loadnpmtasks('grunt-contrib-jshint'); grunt.registertask('default', ['jshint']); };
Run Grunt $ grunt Running "jshint:all" (jshint) task Linting app.js...error [L12:C20] W033: Missing semicolon. var app = express() Warning: Task "jshint:all" failed. Use --force to continue. Aborted due to warnings.
OH NO
Fix & re-run Grunt $ grunt Running "jshint:all" (jshint) task >> 4 files lint free. Done, without errors.
Closing thoughts Node is not for every kind of app Must consider carefully where to use it Callback hell because Async
Bye Follow me on twitter: @PRPATEL