Upload
quhan-arunasalam
View
96
Download
0
Tags:
Embed Size (px)
Citation preview
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.
Node.js
Quhan ArunasalamJune / 03 / 2015 Hackathon@SST
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
What we’re going to explore today
Node.js
NPM
An open source, cross-platform runtime environment for server-side Javascript applications.
A package manager for Javascript.
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Node.jsRun Javascript on the server
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Understanding the Event LoopThe guts of Node
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
DoJSON based REST APIsWeb / Mobile-Web AppsNetwork Apps
Don’t CPU intensive work
When to use Node?
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Test the waters via REPLThe Read-Eval-Print Loop
• Provides a way to interactively run JavaScript and see the results.
• Useful for debugging, testing, or just trying things out.
https://www.flickr.com/photos/snype451/5752753663/
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.1: Test the waters via REPLRead-Eval-Print-Loop
$ node
> var a = [1, 2, 3];
> console.log(a);[ 1, 2, 3 ]
> a.forEach(function (z) { console.log(z); });123
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.2: Baby-stepsBuilding the classic Hello World
$ mkdir hello && cd hello
$ touch index.js
// index.jsconsole.log('Hello SST');
$ node indexHello SST
https://www.flickr.com/photos/munakz/9228501911/
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
The module systemThe building blocks of a Node app
http://pixabay.com/en/lego-building-blocks-shapes-puzzle-297773/
• Makes it possible to include other Javascript files into your app.
• Helps organize your code into separate parts with limited responsibilities.
• Using modules is simple - You just require() them.
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.3: Requiring thingsModifying your previous Hello World example
$ touch greet.js
// greet.jsexports.hello = function () {return 'Hello SST';}
// index.jsvar greet =
require('./greet.js');console.log(greet.hello());
$ node indexHello SST
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.4: Requiring things (again)Let’s get bilingual
// greet.jsexports.hello = function () {return 'Hello SST';}exports.konbanwa = function () {return 'Konbanwa SST';}
// index.jsvar greet =
require('./greet.js');console.log(greet.hello());console.log(greet.konbanwa());
$ node indexHello SSTKonbanwa SST
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.5: Requiring things (one last time)Another way of handling exports
// greet.jsmodule.exports = {
hello: function () { return 'Hello SST'; },
konbanwa: function () { return 'Konbanwa SST'; }
};
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
In-built modulesDon’t worry, we’re getting to the fun parts
http://commons.wikimedia.org/wiki/File:AMC_V8_engine_360_CID_customized_um.JPG
Node ships with a number of core modules. For example:
• console - Sends output to stdout or stderr.
• http - Provides a server and client for HTTP traffic.
• fs - Provides functions to interact with the file system.
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.6: Create a better (Hello) WorldBy building a web server
// index.jsvar http = require('http');var greet = require('./greet.js');
http.createServer(function (req, res) { res.writeHead(200); res.end(greet.hello());}).listen(80);
console.log('Server running at port 80');
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.6: Create a better (Hello) WorldTa-daa!
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
What is npm?It’s 3 things actually
https://www.flickr.com/photos/kamshots/3096111340/
• A module registry, containing a collection of open-source code.
• A standard, to define dependencies on other packages.
• A package manager, for locally installed modules.
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
The npmjs.com registryNote the 134,726 packages available (at the time of screenshot)
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.7: Initializing your Hello World projectWith metadata
$ npm init
...
name: (hello) version: (1.0.0) description: An app to say Hello SSTentry point: (index.js) test command: git repository: keywords: helloworldauthor: Quhan Arunasalamlicense: (ISC)
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.8: Saving the momentInstalling and using a 3rd party module
$ npm install --save moment
// index.jsvar http = require('http');var greet = require('./greet.js');var moment = require('moment');
http.createServer(function (req, res) { res.writeHead(200); res.end('Hi! It is now ' + moment().format('h:mm:ss a'));}).listen(80);
console.log('Server running at port 80');
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.8: Saving the momentTa-daa!
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Figuring out package.json
{ "name": "hello", "version": "1.0.0", "description": "An app to say Hello SST", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "helloworld" ], "author": "Quhan Arunasalam", "license": "ISC", "dependencies": { "moment": "^2.9.0" }}
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Let’s not publish another hello world8814 packages available (at the time of screenshot)
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Let’s not publish another hello worldHiding away your little secrets
{ "name": "hello", "version": "1.0.0",
"private": true, "description": "An app to say Hello
SST", ...}