ExpressJS - HTTP方法

ExpressJS - HTTP方法 首页 / ExpressJS入门教程 / ExpressJS - HTTP方法

请求中提供了HTTP方法,该方法指定了客户端已请求的操作。下表列出了最常用的HTTP方法-

S.No.Method & Remark
1

GET

GET方法用于获取数据

2

POST

POST方法用于提交数据

3

PUT

PUT方法用于修改数据

4

DELETE

DELETE方法用于删除数据

GET Method

GET请求通过在请求的URL部分中指定参数来从Web服务器检索数据。这是用于文档检索的主要方法。下面的示例利用GET方法获取hello.htm:

GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.learnfk.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

针对上述GET请求的服务器响应如下:

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>

HEAD Method

HEAD方法在函数上与GET相似,不同之处在于服务器使用响应行和标头(但没有实体主体)进行回复。下面的示例使用HEAD方法来获取有关hello.htm的头信息:

HEAD /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.learnfk.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

针对上述GET请求的服务器响应如下:

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed

您会注意到,这里的服务器在标头之后不发送任何数据。

POST Method

当您要将一些数据发送到服务器时使用POST方法,例如文件更新,表单数据等。下面的示例利用POST方法将表单数据发送到服务器,它将由服务器处理。 process.cgi,最后将返回响应:

POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.learnfk.com
Content-Type: text/xml; charset=utf-8
Content-Length: 88
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://clearforest.com/">string</string>

服务器端脚本process.cgi处理传递的数据并发送以下响应:

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed
<html>
<body>
<h1>Request Processed Successfully</h1>
</body>
</html>

PUT Method

PUT方法用于请求服务器在给定URL指定的位置存储包含的实体。以下示例请求服务器将给定的实体正文保存在服务器根目录下的hello.htm中:

PUT /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.learnfk.com
Accept-Language: en-us
Connection: Keep-Alive
Content-type: text/html
Content-Length: 182
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>

服务器会将给定的实体正文存储在hello.htm文件中,并将以下响应发送回客户端:

HTTP/1.1 201 Created
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed
<html>
<body>
<h1>The file was created.</h1>
</body>
</html>

DELETE Method

DELETE方法用于请求服务器在给定URL指定的位置删除文件。以下示例要求服务器删除服务器根目录下的给定文件hello.htm:

DELETE /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.Learnfkpoint.com
Accept-Language: en-us
Connection: Keep-Alive

服务器将删除提到的文件hello.htm,并将以下响应发送回客户端:

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed
<html>
<body>
<h1>URL deleted.</h1>
</body>
</html>

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学微服务 -〔胡忠想〕

DDD实战课 -〔欧创新〕

数据中台实战课 -〔郭忆〕

Serverless入门课 -〔蒲松洋(秦粤)〕

小马哥讲Spring AOP编程思想 -〔小马哥〕

成为AI产品经理 -〔刘海丰〕

代码之丑 -〔郑晔〕

Redis源码剖析与实战 -〔蒋德钧〕

深入浅出可观测性 -〔翁一磊〕

好记忆不如烂笔头。留下您的足迹吧 :)