プリンキピア

第11章Webアプリケーション開発

CONCEPTS IN PRACTICE実践の中の概念
APIs Play a Large Role in Web ServicesWebサービスで大きな役割を果たすAPI
Understanding APIs and their protocols helps determine which platform is being accessed and at what level. APIs allow access to any software with a specific purpose or functionality. APIs can be used as contracts between multiple applications.
APIとそのプロトコルを理解すると、どのプラットフォームに、どのレベルでアクセスしているかを判断しやすくなる。APIを使えば、特定の目的や機能を持つソフトウェアにアクセスできる。APIは、複数のアプリケーション間の契約として使うことができる。
How do APIs work? The API architecture is usually explained in terms of client and server. The application sending the request is called the client, and the application sending the response is called the server. For example, in the case of an API for a weather service, the weather service database is maintained on the server side, and the mobile app is running on a client mobile device.
英語のヒント
APIはどのように動作するのだろうか。APIのアーキテクチャは、通常、クライアントとサーバーという形で説明される。要求を送るアプリケーションをクライアントといい、応答を送るアプリケーションをサーバーという。たとえば、天気情報サービスのAPIでは、天気情報のデータベースはサーバー側で管理され、モバイルアプリはクライアントとなるモバイル端末で実行される。
The server side performs the majority of the functionality. It uses a combination of templated HTML, controller and application server technologies (e.g., ASP.NET, C#), and SQL. JavaScript is sent to the browser using jQuery for cross-browser support, or it may use Asynchronous JavaScript and XML to communicate with the web server without refreshing the page. Asynchronous JavaScript and XML (AJAX) exchanges small amounts of data between a client and server. The engineering team using such a web platform must understand and enforce the separation of responsibilities between the MVC components to ensure future modifications, especially significant changes, can be made without rearchitecting the system. Because the engineering team needs to know different programming languages for different layers, there may be some internal resistance to this, and it may be tempting to go around a layer to make a quick fix. Essentially, making sure that the architecture of the web platform is understood and used properly, architectural adherence is predicated on the expertise of the engineering team members and more on the nature of the tools used.
英語のヒント
機能の大部分はサーバー側で実行する。HTMLのテンプレート、Controllerとアプリケーションサーバーの技術(ASP.NET、C#など)、SQLを組み合わせて使う。JavaScriptをブラウザーへ送る際には、複数のブラウザーに対応するためjQueryを使う。また、ページを再読み込みせずにWebサーバーと通信するため、Asynchronous JavaScript and XMLを使うこともある。Asynchronous JavaScript and XML(AJAX)は、クライアントとサーバーの間で少量のデータをやり取りする。このようなWebプラットフォームを使う技術チームは、将来の変更、特に大きな変更を、システムのアーキテクチャを作り直さずに行えるよう、MVCの各コンポーネント間の責任の分離を理解し、徹底しなければならない。技術チームは、異なる層について異なるプログラミング言語を知る必要があるため、内部に抵抗が生じるかもしれない。また、手早く修正するために、ある層を迂回したくなることもある。基本的に、アーキテクチャの遵守、すなわちWebプラットフォームの構造を理解して適切に使うことは、技術チームの各メンバーの専門性に、そしてそれ以上に、使うツールの性質に依存している。

Responsive Web 2.0 ApplicationsレスポンシブなWeb 2.0アプリケーション

By the mid-2000s, JavaScript became popular for client-side browser functionality. AJAX and jQuery technologies allowed developers to create single-page applications (SPA). For the end user, SPAs offered functionality like a traditional desktop application and drastically improved user experience by reducing latency. No longer were expensive web server requests needed that resulted in full page refreshes in the browser. Instead, small data transmissions could be sent and received between the browser and server through AJAX. Figure 11.5 compares the life cycle of SPAs to a traditional Web 2.0 application.
英語のヒント
2000年代半ばまでには、クライアント側のブラウザー機能を実装するために、JavaScriptがよく使われるようになった。AJAXとjQueryによって、開発者はシングルページアプリケーション(SPA)を作成できるようになった。最終利用者にとって、SPAは従来のデスクトップアプリケーションに似た機能を提供し、待ち時間を減らすことで利用体験を大きく改善した。ブラウザーでページ全体を再読み込みすることになる、負担の大きなWebサーバーへの要求は、もはや必要なかった。代わりにAJAXを通じて、ブラウザーとサーバーの間で少量のデータを送受信できた。図11.5は、SPAと従来型のWeb 2.0アプリケーションのライフサイクルを比較している。
Figure 11.5

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 11.5 This is a comparison of the life cycles between traditional Web 2.0 applications and SPAs. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図11.5 従来型のWeb 2.0アプリケーションとSPAのライフサイクルの比較。(出典:Copyright Rice University, OpenStax、CC BY 4.0ライセンス)
LINK TO LEARNING学びを広げるリンク
The World Wide Web Consortium (W3C) (https://openstax.org/r/76W3C) develops standards and guidelines for the Web. You can discover more about them and examine some of their current draft standards.
World Wide Web Consortium(W3C)(https://openstax.org/r/76W3C)は、Webの標準と指針を開発している。この組織についてさらに調べ、現在策定中の標準の草案をいくつか確認できる。
jQuery, an open-source JavaScript library, ensured that web developers could write JavaScript for a generic browser document object model (DOM) that would run regardless of the user’s browser. The DOM is a programming interface provided by browsers. It allows scripts, written in JavaScript for example, to interact with the structure of a web page. When a web page is loaded into the browser, the browser creates a DOM of the page. The DOM structure is a hierarchical treelike structure that organizes the elements of the page as objects. The model used by the DOM enables dynamic access and facilitates the manipulation of content, structure, and style of web pages. Figure 11.6 illustrates a typical SPA architecture where a single web page is delivered to the browser. Note that in this model, the user’s interaction with the site has increased in the amount of content generated by the user.
英語のヒント
オープンソースのJavaScriptライブラリであるjQueryによって、Web開発者は、利用者のブラウザーにかかわらず動作する、共通のブラウザー文書オブジェクトモデル(DOM)向けのJavaScriptを書けるようになった。DOMは、ブラウザーが提供するプログラミング用のインターフェースである。たとえばJavaScriptで書かれたスクリプトから、Webページの構造を操作できる。Webページを読み込むと、ブラウザーはそのページのDOMを作る。DOMは階層的な木構造であり、ページの各要素をオブジェクトとして整理する。DOMのモデルは動的なアクセスを可能にし、Webページの内容・構造・スタイルの操作を容易にする。図11.6は、1枚のWebページをブラウザーに渡す、典型的なSPAのアーキテクチャを示している。このモデルでは、利用者が生成するコンテンツの量という点で、サイトと利用者とのやり取りが増えていることに注目しよう。
Figure 11.6

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 11.6 This illustrates a user’s interaction with a single-page application (SPA). (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図11.6 シングルページアプリケーション(SPA)における利用者のやり取りを示している。(出典:Copyright Rice University, OpenStax、CC BY 4.0ライセンス)
The more sophisticated SPAs required large amounts of JavaScript on the client side. Many end users had underpowered machines or out-of-date web browsers, and the SPAs performed poorly.
英語のヒント
より高度なSPAには、クライアント側で大量のJavaScriptが必要だった。処理能力の低い機械や、古くなったWebブラウザーを使う最終利用者も多く、SPAは十分な性能を発揮できなかった。
In the early 2010s, web application frameworks were introduced to create complex, client-side web applications that performed well, gave a native desktop application-like experience, and were easier for developers to create. These applications followed a Model-View-ViewModel (MVVM) pattern. Figure 11.7 illustrates the relationship between the View, ViewModel, and Model components.
2010年代初めには、複雑なクライアント側のWebアプリケーションを作るためのフレームワークが登場した。こうしたアプリケーションは、十分な性能を発揮し、ネイティブのデスクトップアプリケーションに似た使い心地を提供し、開発者にとって作りやすかった。これらはModel-View-ViewModel(MVVM)パターンに従っていた。図11.7は、View、ViewModel、Modelの各コンポーネントの関係を示している。
Figure 11.7

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 11.7 Various data binding, events, and actions occur between the components of the Model-View-ViewModel pattern. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図11.7 Model-View-ViewModelパターンの各コンポーネント間では、さまざまなデータバインディング、イベント、アクションが生じる。(出典:Copyright Rice University, OpenStax、CC BY 4.0ライセンス)
This pattern is like the MVC pattern we’ve previously explored; however, several key differences exist. The following are some of the similarities and differences:
このパターンは、前に見たMVCパターンに似ているが、いくつかの重要な違いがある。共通点と相違点の一部を、次に示す。
• The View is responsible for the presentation and only interacts with the ViewModel. This is similar in responsibility to the View role and interaction with the Controller in the MVC pattern. The View here binds to functions and properties in the ViewModel and receives notifications on operations and changes to the data. It doesn’t interact directly with the Model.
英語のヒント
• Viewは表示を担当し、ViewModelとだけやり取りする。この責任は、MVCパターンでのViewの役割と、Controllerとのやり取りに似ている。ここでのViewは、ViewModel内の関数やプロパティに結び付けられ、操作やデータの変更についての通知を受ける。Modelとは直接やり取りしない。
• The Model is similar to the Model in the MVC pattern and is responsible for data retrieval and storage. It doesn’t know anything about the ViewModel.
• Modelは、MVCパターンのModelに似ており、データの取得と保存を担当する。ViewModelについては何も知らない。
• The ViewModel is similar to the Controller in that it decouples the relationship between the View and Model and handles data manipulation. The ViewModel responds to notifications from the Model and will send events to the View if needed. Because the View binds to the ViewModel, the ViewModel doesn’t know anything about the View. The ViewModel can work with a local Model in the browser, a remote Model, or both.
英語のヒント
• ViewModelは、ViewとModelの関係を切り離し、データの操作を扱うという点で、Controllerに似ている。ViewModelは、Modelからの通知に応答し、必要に応じてViewへイベントを送る。ViewがViewModelに結び付けられるため、ViewModelはViewについて何も知らない。ViewModelは、ブラウザー内のローカルのModel、遠隔のModel、またはその両方と連携できる。
Unlike the previous MVC pattern regarding server-side rendering, the MVVM pattern is run entirely in the client. A Representational State Transfer (REST) API decouples the client-side Model and ViewModel components from the server-side business logic and persistence store. REST-based (aka RESTful) APIs follow the architecture style designed for the Web. These APIs use the JavaScript Object Notation (JSON) file format that represents data as text-based attribute-value information. Figure 11.8 illustrates the MVVM pattern as it applies to an SPA.
英語のヒント
前述したサーバー側のレンダリングに関するMVCパターンとは異なり、MVVMパターンは、全体がクライアント内で動作する。Representational State Transfer(REST)のAPIは、クライアント側のModelとViewModelを、サーバー側のビジネスロジックと永続化用の記憶領域から切り離す。RESTに基づくAPI(RESTful APIともいう)は、Web向けに設計されたアーキテクチャスタイルに従う。これらのAPIは、データをテキストによる属性と値の情報として表す、JavaScript Object Notation(JSON)ファイル形式を使う。図11.8は、SPAに適用したMVVMパターンを示している。
Figure 11.8

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 11.8 This illustrates the Model-View-ViewModel pattern as applied to the SPA architecture. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図11.8 SPAアーキテクチャに適用したModel-View-ViewModelパターンを示している。(出典:Copyright Rice University, OpenStax、CC BY 4.0ライセンス)
As you can see from the diagram, this model is more complex than the simpler MVC pattern. The quality of the APIs partly determines the effectiveness of this pattern. REST-based APIs benefit from being discoverable (i.e., all API URIs can be found from the root API node) and should be versioned (i.e., to keep track of changes to interfaces for compatibility purposes) so that upgrades don’t break API users. Without API versioning, coupling between client and server increases (due to semantic and syntactic coupling), and upgrades become costly. Similar to a URL, a Uniform Resource Identifier (URI) is a string of characters that identifies a resource on the Web.
英語のヒント
図から分かるように、このモデルは、より単純なMVCパターンよりも複雑である。このパターンがどれだけ有効に働くかは、APIの品質によっても決まる。RESTベースのAPIには、発見可能であること、すなわちルートAPIノードからすべてのAPIのURIを見つけられることの利点がある。また、更新によってAPIの利用者の仕組みが壊れないよう、バージョン管理すべきである。これは、互換性のためにインターフェースの変更を追跡することである。APIのバージョン管理がないと、意味上および構文上の結び付きのために、クライアントとサーバーの結合が強まり、更新の費用がかさむ。Uniform Resource Identifier(URI)は、URLに似ており、Web上のリソースを識別する文字列である。
Regardless of these flaws, early browser-based frameworks proved the value of creating rich client-side apps. Web standards evolved, and newer frameworks emerged that adhered to the newer standards and solved many of the problems of their predecessors. Popular SPA frameworks include Angular, Ember.js, React, and Vue.js. These are often used in conjunction with server-side tools, creating a “full stack” of technologies for developing the solution. A popular server-side technology in this stack is Node, a runtime environment for executing JavaScript code.
英語のヒント
これらの欠点はあったものの、初期のブラウザーベースのフレームワークは、機能の豊かなクライアント側のアプリを作ることの価値を示した。Web標準が進化すると、新しい標準に従い、先行するフレームワークの問題の多くを解決した、新しいフレームワークが登場した。よく使われるSPAフレームワークには、Angular、Ember.js、React、Vue.jsがある。これらはしばしばサーバー側のツールと組み合わせて使われ、解決策を開発するための技術の「フルスタック」を構成する。このスタックでよく使われるサーバー側の技術に、JavaScriptのコードを実行するランタイム環境のNodeがある。
Approaches to minimizing the data transferred between the APIs and the client to reduce network traffic or simplifying access to the data resulted in two primary approaches. One was flattening the data model. Instead of returning data in a nested format (such as from a relational database), the returned data would be “flattened” to a series of key-value pairs, each resulting in a unique return value. If the specific request could be made, the flattened data would be preferred to navigating the nested data to improve performance. However, creating APIs to fit specific requests could be challenging. Another solution was using GraphQL, an open-source query and manipulation language. With GraphQL, callers of its API could craft specific requests to return only the needed data, which made access to many services such as the ones provided by microservices architectures more scalable.
英語のヒント
APIとクライアントの間で転送するデータを最小化してネットワークの通信量を減らす、またはデータへのアクセスを単純にするために、主に2つの手法が生まれた。1つは、データモデルを平坦化する方法である。関係データベースから取得する場合のような入れ子形式でデータを返す代わりに、データを一連のキーと値の組へ「平坦化」し、それぞれが一意の戻り値となるようにする。必要な特定の要求を行えるのであれば、性能を高めるため、入れ子のデータをたどるより、平坦化したデータのほうが好ましい。しかし、特定の要求に合うAPIを作ることは難しい場合がある。もう1つの解決策は、オープンソースのクエリおよび操作言語であるGraphQLを使うことだった。GraphQLでは、APIの呼び出し側が、必要なデータだけを返す具体的な要求を作ることができた。これにより、マイクロサービスアーキテクチャが提供するものなど、多数のサービスへのアクセスを、より拡張しやすくなった。