Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Front-end Web Development Tom Perkins.

Similar presentations


Presentation on theme: "An Introduction to Front-end Web Development Tom Perkins."— Presentation transcript:

1 An Introduction to Front-end Web Development Tom Perkins

2 Disclaimer Examples follow – Pluralsite.com – Learning to Program – Part 1: Getting Started – Scott Allen – Node.js

3 Programs run inside node.js Node – Easy to install – Can run on Mac, Windows, or Linux First thing to do – Install Node

4 Node.js Superfast, stand-alone JavaScript interpreter Can function as a web server Easy to install Started from the Command Prompt JavaScript programs run “inside” Node. Two “run modes” – REPL (Loop) – Execute external JavaScript programs

5 Installing Node Launch any web browser (IE, Chrome, FireFox) Go to nodejs.org Download node Install node (use defaults)

6 Start Node Search your apps for “node” Select the ”Node.js command prompt” This will open the Command Prompt Window

7 The Command Prompt Window Run node by typing node at the command prompt

8 Run node Node runs JavaScript in the REPL loop.

9 Node’s REPL

10 REPL Examples To exit Node, type “.exit” at the command prompt (no quotes)

11 Some Node.js Objects Console – console.log(data) – outputs data to the output device Process – process.argv – returns an array containing command line arguments First element – ‘node’ Second element – name of JavaScript file Next elements – remaining command line arguments

12 Notes for process.argv() // print process.argv process.argv.forEach(function(val, index, array) { console.log(index + ': ' + val); }); This will generate: $ node process-2.js one two=three four 0: node 1: /Users/mjr/work/node/process-2.js 2: one 3: two=three 4: four

13 A Simple JavaScript Program Using Functions Running in Node

14 A Simple JavaScript Program (using functions) Program: \NTPCUG_Node\Functions\ functions.js Code editor: Notepad++ (free download) Steps taken: 1.Create the program in Notepad++ 2.Save the program as functions.js in the folder \NTPCUG_Node\Functions 3.Select the Command Prompt from the Start Menu 4.Enter cd\ntpcug_node\functions 5.Type: node functions.js 6.The program will execute …

15 A program to simulate the roll of a die and to evaluate that roll: 3, 4, 6 -> Great roll! 1 -> Terrible roll! Otherwise -> Ok roll.

16 Structured Walkthru: A JavaScript Program employing Functions

17 A JavaScript Program Demonstrating an Object

18 A JavaScript Program demonstrating a JavaScript Object

19 Structured Walkthru: A JavaScript Program using an Object

20 USING MULTIPLE FILES Objects group related data and behavior into a container called an “object”. To hide complexity – Use objects – Use multiple source code files to build an application A file may contain the definition of one or more objects No limit on number of files in an application

21 Multiple Files Object.js Program.js (main program – controller) dice.js (code related to rolling dice) Both files in the same folder: \ntpcug_node\multiple

22 Loading a file Require keyword – used to load a file All properties and methods are hidden by default in a loaded file Inside a called program, you have to explicitly tell node what you want to make available to calling programs Use the node “exports” object to identify what you want to expose Add a “die” property to the exports object – exports.die = die; To gain access to the exported members in the “required” program, place the “required” object into a variable in the calling program – var dice = require(“./dice”); To access the “die” object in the “dice” object – var die = dice.die;

23 USING MULTIPLE FILES IMPORTANT CONCEPTS: Called program exports object Calling program require keyword Accessing objects w/in called program

24 Structured Walkthru: Using Multiple Files in Javascript

25 A MORE COMPLEX FILE STRUCTURE AND INTRODUCTION TO TESTING

26 A Gradebook Program Given a set of scores for a student (87, 92, 72, 100, etc) calculate the average grade and assign a letter grade. Folder Structure: Gradebook Tests Lib ( application libraries) In c:\ntpcug_node mkdir gradebook In c:\ntpcug_node\gradebook (cd gradebook) mkdir tests mkdir lib

27 Test-Driven Development Nodeunit – a unit testing package for Node.js Install nodeunit using the NPM (Node Package Manager) – in gradebook: > npm install –g nodeunit Nodeunit looks for tests in the “tests” folder No tests available …

28 Tests are assertions about the data in a program Tests are written in the gradebook/test folder They require programs in the /gradebook folder

29 Create a file containing the gradeBook object in the \gradebook\lib folder (grades.js). Create a gradeTests.js file in the \gradebook\tests folder.

30 gradebook folder (tests folder) gradeTests.js lib folder grades.js (gradebook object)

31 Write a test first (in testGrades.js) It will fail (no addGrade method, no count) But that’s OK!

32 Structured Walkthru(s) grades.js – contains gradeBook object gradeTests.js – contains tests (assertions) about methods and properties of gradeBook object program.js – enter test scores from the command prompt line

33 WEBSITE

34 Node as a web server Node.js can function as a web server You can build websites and web applications Expressjs is a scaffolding tool It can be used to build a basic website See expressjs.com

35 Build a website Install expressjs – npm install express Modify website/program.js Start the server –...\website node program.js – Should produce Starting message …

36 Using any web browser, access localhost:3000 – (server is listening on port 3000) Response indicates root was accessed but site was unable to handle request Use CTRL-C to exit the server …

37 Modify program.js to handle an empty request on port 3000 Restart the server Access the site at localhost:3000 See response: Add

38 Pass a number of grades to a different URL Have the server respond with a letter grade – localhost:3000/grade?grades=100,90,80,95 – Query string

39 Server code to handle request of localhost:3000/grade?grades= … Add

40 Final result!

41 FINIS


Download ppt "An Introduction to Front-end Web Development Tom Perkins."

Similar presentations


Ads by Google