Slide 1

Slide 1 text

Node and Grunt What they are, and how to set up a JavaScript project with them

Slide 2

Slide 2 text

Strategy • To get you started with setting up a basic project so that if you are interested, you can teach yourself more, hands-on

Slide 3

Slide 3 text

Node • Server-side or native JavaScript – Not the first, but perhaps the most popular • A whole ecosystem – Fun, cutting-edge technologies • Built on the Chrome’s open-source V8 engine – JIT compilation to native bytecode

Slide 4

Slide 4 text

npm • Node Package Manager – Like NuGet for JavaScript • Dependency model – Local, hierarchical dependencies, e.g.: • My project – Module A, v1.0.1 » Module C, v0.2.3 – Module B, v0.6.2 » Module C, v1.1.0 – Stored in ./node_modules directory – Configured in package.json at project root.

Slide 5

Slide 5 text

Potential Uses of Node • Building native apps in JavaScript • Run the same code inside or outside the browser – E.g. command-line app with Node – Same app with GUI via the browser • Building services in JavaScript – e.g. MEAN technology stack • MongoDB, ExpressJS, AngularJS, NodeJS – Reduced impedance mismatch between frontend and backend – Or fake services, such as our mock API • Headless browser emulation

Slide 6

Slide 6 text

Grunt • Written in pure JavaScript • Task-runner, mostly used as a build tool • You can register tasks from plugins • You can composite those tasks into other tasks • Plugin tasks are highly configurable • All configured in one JavaScript file – Gruntfile.js at project root

Slide 7

Slide 7 text

Example Grunt tasks • Lint (CSS, HTML, JS…) • Compile (Less/Sass -> CSS, CoffeeScript -> JavaScript, Markdown -> HTML…) • Copy files • Concatenate files • Minify JavaScript • Run tests • Watch for changes and rebuild

Slide 8

Slide 8 text

Prerequisites • Install Node and npm – Usually bundled together – Available on Node’s website: http://nodejs.org/ • Use npm to install the Grunt command line – npm install -g grunt-cli – -g is the global flag • Install for the whole system • Not in the individual repository (local) • Usually you won’t want to use this flag

Slide 9

Slide 9 text

Configure your source control • Set up repo on GitHub or BitBucket or… • git clone or git init or hg init or… • If you’ve been meaning to learn Git, maybe this is the time

Slide 10

Slide 10 text

Initialize your npm package • npm init – Interactively asks for the basic info – Creates package.json – You can hand tweak this however you want • Anyone who newly downloads your project can just: npm install – Restores the dependencies

Slide 11

Slide 11 text

Configuring dependencies • Install dependencies – npm install --save ... – If no dependencies, I recommend adding dependencies: {} to the package.json • Development dependencies are separate – npm install --save-dev ... • (In case you are lost, next slide has example)

Slide 12

Slide 12 text

{ "name": “helloworld", "version": "0.0.0", "description": “Hello world in Node.js", "main": “app.js", "scripts": { "test": "grunt specs" }, "author": “Kazark", "license": "MIT", "dependencies": {}, "devDependencies": { "grunt-contrib-jshint": "~0.10.0", "grunt": "~0.4.4", "grunt-contrib-watch": "~0.6.1", "grunt-contrib-jasmine": "~0.6.3" } } Example package.json for an npm project

Slide 13

Slide 13 text

How to not check in dependencies • Make a .gitignore – Ignore node_modules and whatever else • Make npm ignore Git’s ignore file – Add a blank .npmignore

Slide 14

Slide 14 text

Gruntfile layout module.exports = function(grunt) { grunt.initConfig({ /* configuration object */ }); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks… // Register tasks from plug-ins grunt.registerTask('specs', ['jshint', 'jasmine:all']); grunt.registerTask… // Composite tasks from other tasks };

Slide 15

Slide 15 text

module.exports = function(grunt) { grunt.initConfig({ watch: { scripts: { files: [‘helloworld.js'], tasks: ['specs'] } }, jshint: { all: { options: { undef: true, eqeqeq: true }, files: { src: 'helloworld.js' } } }, jasmine: { all: { src: 'helloworld.js', options: { specs: 'helloworld.spec.js', outfile: 'specs.html', keepRunner: true } } }, }); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-jasmine'); grunt.registerTask('specs', ['jshint', 'jasmine:all']); grunt.registerTask('default', ['specs']); grunt.registerTask('dev', ['default', 'watch']); }; Example Gruntfile.js

Slide 16

Slide 16 text

Simple example • npm init • vim package.json • npm install --save-dev grunt-contrib-jasmine grunt-contrib-jshint grunt-contrib-watch • vim .gitignore • touch .npmignore • vim Gruntfile.js • vim –O2 hello.spec.js hello.js • grunt • git add –A • git commit

Slide 17

Slide 17 text

Tips & Ideas • “should exist” tests are particularly helpful – …at a module level – …when bootstrapping your project • Why not try: – Make a small Angular project – Use a different test, spy or assertion framework – Use a language that compiles to JavaScript

Slide 18

Slide 18 text

Discussion • Feel free to comment or ask a question… • …or to leave 

Slide 19

Slide 19 text

A few links • Node: http://nodejs.org/ • npm: https://www.npmjs.org/ • Grunt: http://gruntjs.com/ • Example projects – Node native app https://github.com/Kazark/katasemeion – Node Angular JS project https://github.com/Kazark/conway-game-of-life- with-angular-js