Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
54
|
第
6
章
6.3
 请求头
当跳转到某个页面时,
URL
不是唯一被传到服务器的信息。每次访问某网站时,浏览器都
会发送很多“不可见”的信息。我说的不是让人担忧的个人信息(不过要是浏览器感染了
病毒的话,这可能会发生)。浏览器会告诉服务器,它希望接收的页面是哪种语言的(例
如,如果你的
Chrome
是在西班牙下载的
,
Chrome
就会请求所访问页面的西班牙语版,但
前提是有这个版本)。浏览器还会发送关于
用户代理
(浏览器、操作系统和硬件)的信
息以及其他几项信息。所有这些信息都是以请求头形式发送的,可以通过
request
对象的
header
属性访问。如果好奇想看看浏览器发送的这些信息,可以创建一个简单的
Express
路由用以显示(版本库的
ch06/00-echo-headers.js
):
app.get('/headers', (req, res) => {
res.type('text/plain')
const headers = Object.entries(req.headers)
.map(([key, value]) => `${key}: ${value}`)
res.send(headers.join('\n'))
})
6.4
 响应头
正如浏览器以请求头形式向服务器发送隐藏的信息,服务器响应的时候也会发回一些浏览
器不需要渲染或显示的信息。响应头中的信息一般包含元数据或服务器信息。我们已经
见到了
Content-Type
头,它可以告诉浏览器要传输的内容是什么类型的(
HTML
、图片 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua
前端架构设计

前端架构设计

Micah Godbolt

Publisher Resources

ISBN: 9787115565099