iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 7
1
Modern Web

還在想要買哪一堂線上課程嗎?培養看文件的習慣吧!用 MDN 文件學後端:NodeJS & MongooseDB系列 第 7

七日: Client-Server 概覽 ( 回傳 HTML 之外的東西 , Web 框架 ( frameworks ) 簡化 server-side web 撰寫 )

  • 分享至 

  • xImage
  •  

承上篇來源

處理其他的任務

Web App 的任務是接收 HTTP req 跟回傳 HTTP res 。
與 db 互動來取得或更新資訊是很常見的任務,
server-side code 可能會同時處理其他任務, 或處理時完全不與 db 互動

舉個的例子: Web App 寄發註冊驗證信給 user , 網站可能同時在進行登入或其他作業。

回傳 HTML 之外的東西

如題, Server-side code 不見得只回傳 HTML,
也能回傳動態產生的其他類型檔案 ( text, PDF, ..., etc ),或是資料 ( JSON, XML, ..., etc )

回傳資料給 browser 來動態更新內容 ( AJAX ) 已經行之有年。
最近流行的 SPA ( single-pages apps ) 是整個網站被寫入單頁的 HTML, 且需要時也能夠動態更新。
雖然 SPA 網站執行 server to browser 會有更多衍生的成本,
但可以讓網站的呈現更像一個原生的 App ( highly res, ..., etc )

Web 框架 ( frameworks ) 簡化 server-side web 撰寫

最重要是, frameworks 提供簡單的機制去 map ( 映射 ) 不同 resources/pages URLs,
且可以個別處理的 functions ,使分開串接不同 type ( GET, POST ) 更容易。

也更利於維護, 因為可以只在一處改掉現在的 URL ( 用來傳遞特定目的URL ), 不需要去改處理的 function。

例如, 下方是 Django ( Python ) code , 目的是 map two URL patterns to two view functions
第一個 pattern 確保 /best URL 的 HTTP req 會傳給由一個放在 views module ( 先想像成繪製頁面的一包東西 ) 、 名為 index() 的 function 。
帶有 pattern /best/junior 的 req 將會傳至 junior() 這個 view function

# file: best/urls.py
#

from django.conf.urls import url

from . import views

urlpatterns = [
    \# example: /best/
    url(r'^$', views.index),
    \# example: /best/junior/
    url(r'^junior/$', views.junior),
]

Note: 上方第一個在 url() 裡面的 parameters 或許看起來有點奇怪 (r'^junior/$') , 因為這使用 「正規表達式」(RegEx or RE) ,
現階段我們不需要知道如何使用 RegEx , 只要知道 RegEx 允許我們在 URL 找尋符合的字樣 。
舉例: 找符合 「 一個大寫字母 , 加上後方 4 ~ 7 個小寫字母 」 所組成的單字

Web 框架也使得從 db 拿取資料更容易 , 我們的資料結構被定義在由 db 底層存放 Python classes defined fields 組成的 models :
假設有個被稱作 Team 的 model , 內含一個 team_type field , 我們就可以用簡單的腳本語法去取得特定 type 的 teams (?)

The structure of our data is defined in models, which are Python classes that define the fields to be stored in the underlying database.
If we have a model named Team with a field of "team_type" then we can use a simple query syntax to get back all teams that have a particular type.
-- 上面原句

下方範例是 junior() team_type ( 有大小寫區別 ) list :
filed name ( team_type ) 後方用兩個下底線接一個 match type ( 這裡是 exact ) 。
有很多 matches types 的話 , 我們可以連用 ( chain ) ; 也可以對回傳的結果排序

#best/views.py

from django.shortcuts import render

from .models import Team 


def junior(request):
    list_teams = Team.objects.filter(team_type__exact="junior")
    context = {'list': list_teams}
    return render(request, 'best/index.html', context)

junior() 取得 list 之後 , 它會呼叫 return()
來傳遞原本的 HttpRequest 、 HTML template 、 資料要被放置在 template 的 object ( 在這個例子是 context ) 。
return() 用在 context 與 template 產出的 HTML 非常方便 , 而且回傳的是 HttpRequest object

Web frameworks 可以大量幫助我們處理很多任務 , 後續將會提到更多 frameworks 的好處 , 及現行流行的 framework。

(MDN的)總結

目前為止,我們概覽 server-side code 的運作,還有瞭解 frameworks 的幫助。
接下來,我們會為各位選出最棒的 frameworks ( 這樣寫好嗎? 怎麼好像要引戰XD )


上一篇
六日: Client-Server 概覽( POST req/res 範例 , 解析動態 req )
下一篇
八日: Server-side web frameworks ( 框架能做的事 )
系列文
還在想要買哪一堂線上課程嗎?培養看文件的習慣吧!用 MDN 文件學後端:NodeJS & MongooseDB30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
iT邦新手 2 級 ‧ 2022-04-07 14:55:24

剛好瞄到一個筆誤:

junior() 取得 list 之後 , 它會呼叫 return()

這裡的 return() 應該都是 render()

我要留言

立即登入留言