node.js primer for ite students
TRANSCRIPT
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.
Node.js
Quhan ArunasalamJune / 11 / 2015 ITE@PayPal
© 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
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
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.1: Requiring thingsBuilding the classic Hello World example
Create a folder called hello.
In it, create 2 new files called greet.js and index.js
greet.jsexports.hello = function () {return 'Hello ITE';};
index.jsvar greet = require('./greet.js');console.log(greet.hello());
Run the app using node indexHello ITE
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.2: Requiring things (again)Let’s get bilingual
Modify greet.jsexports.hello = function () {return 'Hello ITE';};exports.konbanwa = function () {return 'Konbanwa ITE';};
Modify index.jsvar greet =
require('./greet.js');console.log(greet.hello());console.log(greet.konbanwa());
Run the app using node indexHello ITEKonbanwa ITE
© 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.3: Create a better (Hello) WorldBy building a web server
Modify index.jsvar http = require('http');var greet = require('./greet.js');
function server(req, res) {res.end(greet.hello());
}
http.createServer(server).listen(8000);console.log('Server running at port 8000');
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.3: Create a better (Hello) WorldTa-daa!
* Press Ctrl+C on your Terminal to stop the app
© 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.4: Initializing your Hello World projectWith metadata
Initialize your app with:npm init
...
name: (hello) version: (1.0.0) description: An app to say Hello ITEentry point: (index.js) test command: git repository: keywords: helloworldauthor: Quhan Arunasalamlicense: (ISC)
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.5: Saving the momentInstalling and using a 3rd party module
Install the moment module using:npm install --save moment
Modify index.jsvar http = require('http');var moment = require('moment');
function server(req, res) { res.end('It is now ' + moment().format('h:mm:ss a'));})
http.createServer(server).listen(8000);console.log('Server running at port 8000');
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary
Lab 2.5: Saving the momentTa-daa!
* Press Ctrl+C on your Terminal to stop the app
© 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 ITE", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "helloworld" ], "author": "Quhan Arunasalam", "license": "ISC", "dependencies": { "moment": "^2.10.3" }}