国产gaysexchina男同gay,japanrcep老熟妇乱子伦视频,吃奶呻吟打开双腿做受动态图,成人色网站,国产av一区二区三区最新精品

Server-side web frameworks

2018-05-15 17:26 更新
先決條件: 基本的計(jì)算機(jī)素養(yǎng)。 高級(jí)了解服務(wù)器端代碼如何處理和響應(yīng)HTTP請(qǐng)求(請(qǐng)參見客戶端 - 服務(wù)器概述)。
目的: 了解Web框架如何簡(jiǎn)化服務(wù)器端代碼的開發(fā)/維護(hù),并讓讀者考慮為自己的開發(fā)選擇一個(gè)框架。

以下部分說(shuō)明了使用從真實(shí)Web框架中提取的代碼片段的一些點(diǎn)。 不要擔(dān)心,所有現(xiàn)在有意義; 我們將通過(guò)我們的框架特定模塊中的代碼來(lái)幫助您。

概述

下一部分提供了有關(guān)Web框架如何簡(jiǎn)化Web應(yīng)用程序開發(fā)的更多詳細(xì)信息。 然后,我們解釋一些可用于選擇Web框架的標(biāo)準(zhǔn),然后列出一些選項(xiàng)。

Web框架可以為您做什么?

Web框架提供了工具和庫(kù)來(lái)簡(jiǎn)化常見的Web開發(fā)操作。 您不是使用服務(wù)器端網(wǎng)絡(luò)框架,但強(qiáng)烈建議 - 這將使你的生活更容易。

本節(jié)討論了Web框架常常提供的一些功能(并非每個(gè)框架都必須提供所有這些功能!)

直接與HTTP請(qǐng)求和響應(yīng)一起工作

正如我們?cè)谏弦黄恼轮锌吹降?,Web服務(wù)器和瀏覽器通過(guò)HTTP協(xié)議進(jìn)行通信 - 服務(wù)器等待來(lái)自瀏覽器的HTTP請(qǐng)求,然后返回HTTP響應(yīng)中的信息。 Web框架允許您編寫簡(jiǎn)化的語(yǔ)法,生成服務(wù)器端代碼以處理這些請(qǐng)求和響應(yīng)。 這意味著你將有一個(gè)更容易的工作,與更容易,更高級(jí)的代碼,而不是低級(jí)網(wǎng)絡(luò)原語(yǔ)交互。

下面的示例顯示了如何在Django(Python)Web框架中工作。 每個(gè)"視圖"函數(shù)(請(qǐng)求處理程序)接收包含請(qǐng)求信息的 HttpRequest 對(duì)象,并且需要返回帶有格式化輸出的 HttpResponse 對(duì)象 )。

# Django view function
from django.http import HttpResponse

def index(request):
    # Get an HttpRequest (request)
    # perform operations using information from the request.
?   # Return HttpResponse
    return HttpResponse('Output string to return')

將請(qǐng)求路由到相應(yīng)的處理程序

大多數(shù)網(wǎng)站會(huì)提供多種不同的資源,可通過(guò)不同的網(wǎng)址訪問(wèn)。 在一個(gè)函數(shù)中處理這些都很難維護(hù),因此Web框架提供了簡(jiǎn)單的機(jī)制來(lái)將URL模式映射到特定的處理函數(shù)。 這種方法在維護(hù)方面也有好處,因?yàn)槟梢愿挠糜谔峁┨囟üδ艿腢RL,而無(wú)需更改底層代碼。

不同的框架使用不同的機(jī)制進(jìn)行映射。 例如,F(xiàn)lask(Python)web框架使用裝飾器向視圖函數(shù)添加路由。

@app.route("/")
def hello():
    return "Hello World!"

雖然Django希望開發(fā)人員定義URL模式和視圖函數(shù)之間的URL映射列表。

urlpatterns = [
    url(r'^$', views.index),
    # example: /best/myteamname/5/
    url(r'^(?P<team_name>\w.+?)/(?P<team_number>[0-9]+)/$', views.best),
]

方便地訪問(wèn)請(qǐng)求中的數(shù)據(jù)

可以以多種方式在HTTP請(qǐng)求中對(duì)數(shù)據(jù)進(jìn)行編碼。 從服務(wù)器獲取文件或數(shù)據(jù)的HTTP GET 請(qǐng)求可能會(huì)編碼URL參數(shù)或URL結(jié)構(gòu)中需要哪些數(shù)據(jù)。 用于更新服務(wù)器上的資源的HTTP POST 請(qǐng)求將在請(qǐng)求的主體內(nèi)包括更新信息作為"POST數(shù)據(jù)"。 HTTP請(qǐng)求還可以包括關(guān)于客戶端側(cè)cookie中的當(dāng)前會(huì)話或用戶的信息。

Web框架提供了編程語(yǔ)言適當(dāng)?shù)臋C(jī)制來(lái)訪問(wèn)這些信息。 例如,Django傳遞給每個(gè)視圖函數(shù)的 HttpRequest 對(duì)象包含用于訪問(wèn)目標(biāo)URL的方法和屬性,請(qǐng)求的類型(例如HTTP GET ), > GET POST 參數(shù),cookie和會(huì)話數(shù)據(jù)等。通過(guò)在URL映射器中定義"捕獲模式",Django還可以傳遞編碼在URL結(jié)構(gòu)中的信息 代碼片段在上面的部分)。

抽象和簡(jiǎn)化數(shù)據(jù)庫(kù)訪問(wèn)

網(wǎng)站使用數(shù)據(jù)庫(kù)來(lái)存儲(chǔ)要與用戶和用戶共享的信息。 Web框架通常提供抽象數(shù)據(jù)庫(kù)讀取,寫入,查詢和刪除操作的數(shù)據(jù)庫(kù)層。 該抽象層被稱為對(duì)象關(guān)系映射器(ORM)。

使用ORM有兩個(gè)好處:

  • You can replace the underlying database without necessarily needing to change the code that uses it. This allows developers to optimise for the characteristics of different databases based on their usage.
  • Basic validation of data can be implemented within the framework. This makes it easier and safer to check that data is stored in the correct type of database field, has the corrrect format (e.g. an email address), and isn't malicious in any way (crackers can use certain patterns of code to do bad things such as deleting database records).

例如,Django web框架提供了一個(gè)ORM,并且將用于定義記錄結(jié)構(gòu)的對(duì)象稱為模型 模型指定要存儲(chǔ)的字段類型,其可以提供對(duì)可以存儲(chǔ)什么信息(例如,電子郵件字段將僅允許有效的電子郵件地址)的字段級(jí)驗(yàn)證。 字段定義還可以指定它們的最大大小,默認(rèn)值,選擇列表選項(xiàng),文檔的幫助文本,表單的標(biāo)簽文本等。模型不陳述關(guān)于底層數(shù)據(jù)庫(kù)的任何信息,因?yàn)檫@是可以改變的配置設(shè)置 分開我們的代碼。

下面的第一個(gè)代碼片段為 Team 對(duì)象顯示了一個(gè)非常簡(jiǎn)單的Django模型。 這將團(tuán)隊(duì)名稱和團(tuán)隊(duì)級(jí)別存儲(chǔ)為字符字段,并指定要為每個(gè)記錄存儲(chǔ)的最大字符數(shù)。 team_level 是一個(gè)選擇字段,因此我們還提供了要顯示的選擇和要存儲(chǔ)的數(shù)據(jù)之間的映射以及默認(rèn)值。

#best/models.py

from django.db import models 

class Team(models.Model): 
?   team_name = models.CharField(max_length=40) 

? ? TEAM_LEVELS = (
? ? ? ? ('U09', 'Under 09s'),
? ? ? ? ('U10', 'Under 10s'),
? ? ? ? ('U11, 'Under 11s'),
?       ...  #list our other teams
? ? )
? ? team_level = models.CharField(max_length=3,choices=TEAM_LEVELS,default='U11')

Django模型提供了一個(gè)用于搜索數(shù)據(jù)庫(kù)的簡(jiǎn)單查詢API。 這可以使用不同的標(biāo)準(zhǔn)(例如,精確,不區(qū)分大小,大于等)一次匹配多個(gè)字段,并且可以支持復(fù)雜語(yǔ)句(例如,您可以在具有團(tuán)隊(duì)的U11團(tuán)隊(duì)上指定搜索 以"Fr"開頭或以"al"結(jié)尾的名稱)。

第二個(gè)代碼片段顯示了一個(gè)用于顯示所有U09團(tuán)隊(duì)的視圖函數(shù)(資源處理程序)。 在這種情況下,我們指定要過(guò)濾所有記錄,其中 team_level 字段正好是文本\'U09\'(請(qǐng)注意下面如何將此條件傳遞給 filter >函數(shù)作為參數(shù),字段名稱和匹配類型由雙下劃線分隔: team_level__exact )。

#best/views.py

from django.shortcuts import render
from .models import Team 

def youngest(request):
? ? list_teams = Team.objects.filter(team_level__exact="U09")
? ? context = {'youngest_teams': list_teams}
? ? return render(request, 'best/index.html', context)

渲染數(shù)據(jù)

Web框架通常提供模板系統(tǒng)。 這些允許您指定輸出文檔的結(jié)構(gòu),為生成頁(yè)面時(shí)要添加的數(shù)據(jù)使用占位符。 模板通常用于創(chuàng)建HTML,但也可以創(chuàng)建其他類型的文檔。

Web框架通常提供一種機(jī)制,可以輕松地從存儲(chǔ)的數(shù)據(jù)生成其他格式,包括 JSON 和 XML

例如,Django模板系統(tǒng)允許您使用"雙句柄"語(yǔ)法(例如 { { variable_name } } ),它將被渲染頁(yè)面時(shí)從視圖函數(shù)傳遞的值替換。 模板系統(tǒng)還支持表達(dá)式(具有語(yǔ)法: {% expression %} ),允許模板執(zhí)行簡(jiǎn)單操作,例如迭代傳遞到模板中的列表值。

注意:許多其他模板系統(tǒng)使用類似的語(yǔ)法,例如:Jinja2(Python),handlebars(JavaScript),mustache(JavaScript)等。

下面的代碼段顯示了如何工作。 繼續(xù)上一節(jié)中的"最新團(tuán)隊(duì)"示例,HTML模板通過(guò)視圖傳遞一個(gè)名為 youngest_teams 的列表變量。 在HTML框架內(nèi)部,我們有一個(gè)表達(dá)式,首先檢查 youngest_teams 變量是否存在,然后在 for 循環(huán)中迭代它。 在每次迭代時(shí),模板在列表項(xiàng)中顯示團(tuán)隊(duì)的 team_name 值。

#best/templates/best/index.html

<!DOCTYPE html>
<html lang="en">
<body>

 {% if youngest_teams %}
? ? <ul>
? ? {% for team in youngest_teams %}
? ? ? ? <li>{{ team.team_name }}</li>
? ? {% endfor %}
? ? </ul>
{% else %}
? ? <p>No teams are available.</p>
{% endif %}

</body>
</html>

如何選擇Web框架

許多Web框架存在幾乎每一種可能需要使用的編程語(yǔ)言(我們列出了幾個(gè)更流行的框架在下一節(jié))。 有了這么多的選擇,它可能變得難以確定什么框架為您的新的Web應(yīng)用程序提供了最好的起點(diǎn)。

可能影響您決定的一些因素是:

  • Effort to learn: The effort to learn a web framework depends on how familiar you are with the underlying programming language, the consistency?of its API, the quality of its documentation, and the size and activity of its community. If you're starting from absolutely no programming experience then consider Django (it is one of the easiest to learn based on the above criteria). If you are part of a development team that already has significant experience with a particular web framework or programming language, then it makes sense to stick with that.
  • Productivity: Productivity is a measure of how quickly you can create new features once you are familiar with the framework, and includes?both the effort to write and maintain code (since you can't write new features while old ones are broken). Many of the factors affecting productivity are similar to those for "Effort to learn" — e.g. documentation, community, programming experience, etc. — other factors include:
    • Framework purpose/origin: Some web frameworks were initially created to solve certain types of problems, and remain?better at creating web apps with similar constraints. For example, Django was created to?support development of a newspaper website, so is good for blogs and other sites that involve publishing things. By contrast, Flask is a much lighter-weight framework and is great for creating web apps running on embedded devices.
    • Opinionated vs unopinionated: An opinionated framework is one in which there are?recommended "best" ways to solve a particular problem. Opinionated frameworks tend to be more productive when you're trying to solve common problems, because they lead you in the right direction, however they are sometimes less flexible.
    • Batteries included vs. get it yourself: Some web frameworks include tools/libraries that address every problem their developers can think "by default", while more lightweight frameworks expect web developers to pick and choose solution to problems from separate libraries (Django is an example of the former, while Flask is an example of a very light-weight framework). Frameworks that include everything are often easier to get started with because you already have everything you need, and the chances are that it is well integrated and well documented. However if a smaller framework has everything you (will ever) need then it can run in more constrained environments and will have a smaller and easier subset of things to learn.
    • Whether or not the framework encourages good development practices: For example, a framework that encourages a Model-View-Controller architecture to separate code into logical functions will result in more maintainable code than one that has no expectations on developers. Similarly, framework design can have a large impact on how easy it is to test and re-use code.
  • Performance of the framework/programming language: Usually "speed" is not the biggest factor in selection because even relatively slow runtimes like Python are more than "good enough" for mid-sized sites running on moderate hardware. The perceived speed benefits of another language, e.g. C++ or JavaScript, may well be offset by the costs of learning and maintenance.
  • Caching support: As your website becomes more successful then you may find that it can no longer cope with the number of requests it is receiving as users access it. At this point you may consider adding support for caching. Caching is an optimisation where you store all or part of a web request so that it?does not have to be recalculated on subsequent requests. Returning a cached request is much faster than calculating one in the first place. Caching can be implemented in your code or in the server (see reverse proxy). Web frameworks will have different levels of support for defining what content can be cached.
  • Scalability: Once your website is fantastically successful you will exhaust the benefits of caching and even reach the limits of vertical scaling (running your web application on more powerful hardware). At this point you may need to scale horizontally (share the load by distributing your site across a number of web servers and databases) or scale "geographically" because some of your customers are based a long way away from your server. The web framework you choose can make a big difference on how easy it is to scale your site.
  • Web security: Some web frameworks provide better support for handling common web attacks. Django for example sanitises all user input from HTML templates so that user-entered JavaScript cannot be run. Other frameworks provide similar protection, but it is not always enabled by default.

還有許多其他可能的因素,包括許可,無(wú)論框架是否在積極發(fā)展等。

如果你是編程的絕對(duì)初學(xué)者,那么你可能會(huì)選擇基于"易于學(xué)習(xí)"的框架。 除了語(yǔ)言本身的"易用性"之外,高質(zhì)量的文檔/教程和活躍的社區(qū)幫助新用戶是您最寶貴的資源。 我們選擇了 Django (Python)和 ="external"> Express (Node / JavaScript)在后面的課程中寫我們的例子,主要是因?yàn)樗鼈內(nèi)菀讓W(xué)習(xí)和有良好的支持。

注意:讓我們?cè)L問(wèn) Django (Python)和 ="http://expressjs.com/"class ="external"> Express (Node / JavaScript),并查看他們的文檔和社區(qū)。

  1. Nativate to the main sites (linked above)
    • Click on the Documentation menu links (named things like "Documentation, Guide, API Reference, Getting Started".
    • Can you see topics showing how to set up URL routing, templates, and databases/models?
    • Are the documents clear
  2. Navigate to mailing lists for each site (accessible from Community links).
    • How many questions have been posted in the last few days
    • How many have responses.
    • Do they have an active community?

幾個(gè)好的網(wǎng)絡(luò)框架?

讓我們繼續(xù),討論幾個(gè)特定的服務(wù)器端Web框架。

下面的服務(wù)器端框架代表了寫作時(shí)最流行的幾個(gè)。 他們都有你需要的一切,以生產(chǎn)力 - 他們是開源,正在積極發(fā)展,有熱情的社區(qū)創(chuàng)建文檔和幫助用戶討論板,并用于大量的高調(diào)網(wǎng)站。 有許多其他偉大的服務(wù)器端框架,你可以使用基本的互聯(lián)網(wǎng)搜索發(fā)現(xiàn)。

注意:說(shuō)明來(lái)自(部分)來(lái)自框架網(wǎng)站!

Django(Python)

Django 是一個(gè)高級(jí)Python Web框架,它鼓勵(lì)快速開發(fā)和干凈,務(wù)實(shí)的設(shè)計(jì)。 由經(jīng)驗(yàn)豐富的開發(fā)人員構(gòu)建,它需要處理大量的網(wǎng)絡(luò)開發(fā)麻煩,所以你可以專注于編寫你的應(yīng)用程序,而不需要重新發(fā)明輪子。 它是免費(fèi)和開源的。

Django遵循"包括電池"的理念,并提供幾乎所有大多數(shù)開發(fā)人員可能想做的"開箱即用"。 因?yàn)橐磺卸及ㄔ趦?nèi),它一起工作,遵循一致的設(shè)計(jì)原則,并有廣泛和最新的文檔。 基于Python,Django代碼易于閱讀和維護(hù)。 Django的主要優(yōu)點(diǎn)是:

Ridiculously fast

Django旨在幫助開發(fā)人員盡快將應(yīng)用程序從概念到完成。

Reassuringly secure

Django認(rèn)真對(duì)待安全性,并幫助開發(fā)人員避免許多常見的安全錯(cuò)誤。

Exceedingly scalable
Some of the busiest sites on the Web leverage Django’s ability to quickly and flexibly scale.

使用Django(來(lái)自Django主頁(yè))的熱門網(wǎng)站包括:Disqus,Instagram,Knight基金會(huì),MacArthur基金會(huì),Mozilla,國(guó)家地理,開放知識(shí)基金會(huì),Pinterest,開放堆棧。

Flask(Python)

Flask 是Python的微框架。

雖然簡(jiǎn)約,F(xiàn)lask可以創(chuàng)建嚴(yán)肅的網(wǎng)站開箱。 它包含開發(fā)服務(wù)器和調(diào)試器,并且包括對(duì) Jinja2 模板,安全Cookie, https://en.wikipedia.org/wiki/Unit_testing"class ="external">單元測(cè)試 ="external"> RESTful 請(qǐng)求分派。 它有良好的文檔和活躍的社區(qū)。

Flask已經(jīng)變得非常受歡迎,特別是對(duì)于需要在小型,資源受限的系統(tǒng)上提供Web服務(wù)的開發(fā)人員(例如,在 "> Raspberry Pi ,無(wú)人機(jī)控制器等。 )

Express(Node.js / JavaScript)

Express 是一個(gè)快速,無(wú)庸置疑,靈活和簡(jiǎn)約的網(wǎng)絡(luò)框架,用于 en /"class ="external"> Node.js (節(jié)點(diǎn)是運(yùn)行JavaScript的無(wú)瀏覽器環(huán)境)。 它為Web和移動(dòng)應(yīng)用程序提供了一組強(qiáng)大的功能,并提供了有用的HTTP實(shí)用程序方法和中間件 。

Express非常受歡迎,部分原因是它簡(jiǎn)化了客戶端JavaScript Web程序員在服務(wù)器端開發(fā)中的遷移,部分是因?yàn)樗琴Y源高效的(基礎(chǔ)節(jié)點(diǎn)環(huán)境在線程中使用輕量級(jí)多任務(wù),而不是為每個(gè)線程創(chuàng)建單獨(dú)的進(jìn)程 新的Web請(qǐng)求)。

因?yàn)镋xpress是一個(gè)簡(jiǎn)約的Web框架,它不包含您可能想要使用的每個(gè)組件(例如,數(shù)據(jù)庫(kù)訪問(wèn)和對(duì)用戶和會(huì)話的支持通過(guò)獨(dú)立的庫(kù)提供)。 有許多優(yōu)秀的獨(dú)立組件,但有時(shí)可能很難找到,這是最好的特定目的!

許多流行的服務(wù)器端和全棧框架(包括服務(wù)器端和客戶端框架)都基于Express,包括羽毛 , ItemsAPI , KeystoneJS , Kraken , external"> LEAN-STACK , LoopBack MEAN Sails 。

許多知名公司使用Express,包括:Uber,Accenture,IBM等(提供了一個(gè)列表 ="external">此處)。

Ruby on Rails(Ruby)

Rails (通常稱為"Ruby on Rails")是為Ruby編程語(yǔ)言編寫的Web框架。

Rails遵循與Django非常相似的設(shè)計(jì)理念。 像Django一樣,它提供了標(biāo)準(zhǔn)機(jī)制,用于路由URL,從數(shù)據(jù)庫(kù)訪問(wèn)數(shù)據(jù),從模板生成HTML并將數(shù)據(jù)格式化為 ="glossaryLink"> JSON XML 。 它同樣鼓勵(lì)使用像DRY("不重復(fù)自己" - 只寫一次代碼,如果可能的話),MVC(模型 - 視圖控制器)和許多其他設(shè)計(jì)模式。

由于具體的設(shè)計(jì)決定和語(yǔ)言的性質(zhì),當(dāng)然有許多差異。

Rails已用于高度重視的網(wǎng)站,包括: Basecamp , ://github.com/"class ="external"> GitHub , Shopify , "https://airbnb.com/"class ="external"> Airbnb Twitch , class ="external"> SoundCloud Hulu , Zendesk , Square >,高層

ASP.NET

ASP.NET 是由Microsoft開發(fā)的用于構(gòu)建現(xiàn)代Web應(yīng)用程序和服務(wù)的開源網(wǎng)絡(luò)框架。 使用ASP.NET,您可以快速創(chuàng)建基于HTML,CSS和JavaScript的網(wǎng)站,擴(kuò)展它們供數(shù)百萬(wàn)用戶使用,并輕松添加更復(fù)雜的功能,如Web API,數(shù)據(jù)形式或?qū)崟r(shí)通信。

ASP.NET的區(qū)別之一是它建立在公共語(yǔ)言運(yùn)行時(shí)(CLR) ,允許程序員使用任何支持的.NET語(yǔ)言(C#,Visual Basic等)編寫ASP.NET代碼。 像許多Microsoft產(chǎn)品一樣,它受益于優(yōu)秀的工具(通常是免費(fèi)的),活躍的開發(fā)者社區(qū)和精心撰寫的文檔。

ASP.NET由Microsoft,Xbox.com,Stack Overflow和許多其他人使用。

概要

本文展示了Web框架可以使開發(fā)和維護(hù)服務(wù)器端代碼變得更加容易。 它還提供了一些流行框架的高級(jí)概述,并討論了選擇Web應(yīng)用程序框架的標(biāo)準(zhǔn)。 您現(xiàn)在至少應(yīng)該有一個(gè)如何選擇一個(gè)Web框架為您自己的服務(wù)器端開發(fā)的想法。 如果沒有,那么不要擔(dān)心 - 稍后我們將給你詳細(xì)的Django和Express教程,給你一些實(shí)際使用web框架的經(jīng)驗(yàn)。

對(duì)于本單元中的下一篇文章,我們將稍微改變方向并考慮網(wǎng)絡(luò)安全。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)