1. Trang chủ
  2. » Giáo án - Bài giảng

ajax node js

26 397 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Ajax & Node.js
Người hướng dẫn Roger Whitney
Trường học San Diego State University
Chuyên ngành Emerging Web and Mobile Technologies
Thể loại Essay
Năm xuất bản 2011
Thành phố San Diego
Định dạng
Số trang 26
Dung lượng 205,9 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

2 jQuery Documentation, http://api.jquery.com/jQuery.get/ Ajax Programming, http://en.wikipedia.org/wiki/Ajax_programming XMLHttprequest, http://en.wikipedia.org/wiki/XMLHttpRequest Node

Trang 1

CS 696 Emerging Web and Mobile Technologies

Spring Semester, 2011 Doc 13 Ajax & Node.js

Mar 8, 2011

Copyright ©, All rights reserved 2011 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA OpenContent (http:// www.opencontent.org/opl.shtml) license defines the copyright on this document.

Tuesday, March 8, 2011

Trang 2

2

jQuery Documentation, http://api.jquery.com/jQuery.get/

Ajax Programming, http://en.wikipedia.org/wiki/Ajax_(programming) XMLHttprequest, http://en.wikipedia.org/wiki/XMLHttpRequest

Node.js Documentation, http://nodejs.org/docs/v0.4.2/api/

Up and Running with Node.js, Tom Hughes-Croucher, http://ofps.oreilly.com/titles/9781449398583/ index.html

Wikipedia, as noted on individual slides

Tuesday, March 8, 2011

Trang 3

3

Many Web and Mobile Apps require data from serverPhoneGap does not provide direct access to network socketHow to get data from server

Tuesday, March 8, 2011

Trang 5

code (scripts)

Tuesday, March 8, 2011

Trang 6

JSON - JavaScript Object Notation

"strings"

12.345 (numbers)12

12.3e12arraysobjects

array[12, "cat", true]

["this", "is", "an array"]

object{"key":"value"}

{"name":"Roger", "age": 12}

{"colors: ["red","black", "blue"]}

Tuesday, March 8, 2011

Trang 7

JQuery XMLHttpRequest Shortcuts

data - data returned by servertextStatus -

jqXHR - superset of XMLHttpRequest object

dataType - type of data requested, optional

xml, json, script, html

Tuesday, March 8, 2011

Trang 8

Equivalent Versions

8

jQuery.get( url, [ data ], [ success(data, textStatus, jqXHR) ], [ dataType ] )

$.get( url, [ data ], [ success(data, textStatus, jqXHR) ], [ dataType ] )

$.ajax({

url: url, data: data, success: success, dataType: dataType});

Tuesday, March 8, 2011

Trang 9

9

$.get('ajax/test.html', function(data) { $('.result').html(data);

alert('Load was performed.');

});

Tuesday, March 8, 2011

Trang 10

Sample Requests & Server

Trang 11

Server - Handling Requests

11

There are many different systems to handle web requests

php

18+ frameworks.net

7+ frameworksperl

6+ frameworksJava

34+ frameworksRuby

6+ frameworksPython

15+ frameworksSeaside (Smalltalk)Kepler (Lua)

Lift (Scala)Nitrogen (Erlang)etc

Tuesday, March 8, 2011

http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks

Trang 12

12

JavaScript on desktop/server sideEvent-driven non-blocking I/O frameworkScalable network programs

Uses Googles V8 JavaScript Engine

Compiles JavaScript to machine codeUsed in HP's WebOS Phones and tablets

http://nodejs.org/

Tuesday, March 8, 2011

Trang 13

Up and Running with Node.js

13

Early draft of Node.js book by Tom Hughes-Croucher

http://ofps.oreilly.com/titles/9781449398583/index.html

Tuesday, March 8, 2011

Trang 14

Delays in I/O

14

To read a file with blocking I/O

Get File descriptor from file name

"Open" file for readingWait for hard drive head to get to correct locationWait as hard drive spins to read file contents

May require multiple movement of hard drive headWhile this happens your code waits

Tuesday, March 8, 2011

Trang 15

15

Common way to scale performanceWhile one thread is blocked on I/O another thread can perform workTypical server

One high priority thread accepts connects from clientsOnce accepted a client connection is give to a worker thread

Tuesday, March 8, 2011

Trang 16

Thread Issues

16

Overhead

MemoryTime in context switchesManaging threads

Programming issues

Communication between threadsDeadlock

LivelockMultiple threads accessing same data

Tuesday, March 8, 2011

Trang 17

Instead uses callbacks

When OS has data for you to read you callback function is called

Tuesday, March 8, 2011

*Well it does have proccesses and will use WebWorkers

Trang 18

Reading a File

18

var fs = require('fs');

function processFileContents(error, data) {

if (error) throw error;

console.log(data);

}fs.readFile('Client.html','utf8', processFileContents );

Tuesday, March 8, 2011

Node.js does support synchronous reading of files

Trang 19

Second Example - Reading Chunks

Trang 21

Simple WebServer - Standard Example

Trang 22

Complete Example

22

Use clicks on "Cats" or "Cars" buttonConnect to server to get list of "Cats" or "Cars"Add list to next page

Display new page with list

Tuesday, March 8, 2011

Trang 23

<a href="#result" data-role="button" data-inline="true" onclick="sendDatắcats')">Cats</a>

<a href="#result" data-role="button" data-inline="true" onclick="sendDatắcars')">Cars</a>

Trang 24

Client Side JavaScript

var startList = '<ul data-role="listview" data-theme="a"><li>';

var endList = '</li></ul>';

var listContents = array.join("</li><li>");

return startList + listContents + endList;

}

</script>

Tuesday, March 8, 2011

Trang 25

function handleRequest(request, response) {

var category = requestedCategory(request.url);

var requestedData = dataFor(category);

returnData(requestedData, response);

}

function returnData(data, response) { response.writeHead(200, {'Content-Type': 'application/json'}); response.end(data);

}

Tuesday, March 8, 2011

Ngày đăng: 28/04/2014, 16:39

Xem thêm

TỪ KHÓA LIÊN QUAN

w