http-console是一个Node模块,为您提供用于执行HTTP命令的命令行界面。 无论是针对Web服务器,Web服务,甚至数据库服务器发出的HTTP请求,它都非常适合调试和查看HTTP请求到底发生了什么。
安装
要使用http-console,您需要安装Node。 如果尚未安装,请直接转到http://nodejs.org并下载适用于您的操作系统的安装程序,或者如果希望通过软件包管理器进行安装 ,请直接转到Node Wiki。
接下来,使用npm安装http-console:
$> npm install http-console2 -g
需要注意的几件事:
- 我们实际上是在安装http-console2 ,而不是http-console 。 http-console2是http-console的一个分支,但包含一个修复程序,该错误由在较新版本的Node中弃用
require.paths
引起。 它以http-console2的形式发布到npm,但是一旦安装,您仍然可以以http-console的身份运行它。 我们正在使用
-g
全局开关安装http-console2。 这意味着您可以从任何地方调用http-console,因为它安装在$PATH
中的某个位置:$> type http-console http-console is /usr/local/bin/http-console
要开始使用http-console,我们只需将要连接的URL和端口传递给它,然后开始发出HTTP命令。
说HTTP
让我们连接到服务器并发出一些命令。 首先,我们将使事情变得简单,并向Web服务器发出一些GET
请求。 我假定您在阅读本文时,是一名Web开发人员。 而且,作为Web开发人员,您可能已经在http:// localhost上运行了Web服务器。 通过输入以下内容,告诉http-console连接到它:
$> http-console http://localhost
> http-console 0.6.1
> Welcome, enter .help if you're lost.
> Connecting to localhost on port 80.
现在,您已连接,可以开始发出命令了。 在提示符下键入GET /
:
http://localhost:80/> GET /
HTTP/1.1 200 OK
Server: nginx/1.0.11
Date: Wed, 04 Jan 2012 08:40:04 GMT
Content-Type: text/html
Content-Length: 151
Last-Modified: Mon, 04 Oct 2004 15:04:06 GMT
Connection: keep-alive
Accept-Ranges: bytes
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
我们返回完整的HTTP响应,包括HTTP标头和HTML本身。 您可以通过输入.q
退出http-console。
让我们尝试另一个命令。 最近,我写了有关Node的快速Web框架的文章 ,我们创建了一个页面来显示提及Sitepoint的十条最新推文。 我想知道如果我们使用http-console来查询Twitter的Search API中的类似推文会怎样?
$> http-console http://search.twitter.com
> http-console 0.6.1
> Welcome, enter .help if you're lost.
> Connecting to search.twitter.com on port 80.
现在对/search.json?q=sitepoint&rpp=10
发出GET
请求:
http://search.twitter.com:80/> GET /search.json?q=sitepoint&rpp=10
HTTP/1.1 200 OK
Cache-Control: max-age=15, must-revalidate, max-age=300
Expires: Fri, 17 Feb 2012 22:04:02 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 7749
Vary: Accept-Encoding
Date: Fri, 17 Feb 2012 21:59:02 GMT
X-Varnish: 2065334673
Age: 0
Via: 1.1 varnish
Server: tfe
{
page: 1,
since_id: 0,
max_id_str: '170628259464216576',
refresh_url: '?since_id=170628259464216576&q=sitepoint',
completed_in: 0.107,
results: [
{
to_user_id_str: null,
to_user_name: null,
id: 170628259464216580,
iso_language_code: 'en',
...
同样,我们返回HTTP标头,但是这次我们以JSON形式获取HTTP响应的正文(省略了完整的JSON以节省空间)。
但是我们不限于使用http-console连接到Web服务器和Web服务。 我们还可以使用它连接到提供RESTful API的数据库服务器,例如CouchDB 。 (如果未安装CouchDB,则最简单的启动和运行方法是克隆https://github.com/iriscouch/build-couchdb并按照README.md中的说明进行操作)。
假设CouchDB正在运行(如果您是通过build-couchdb安装的,则启动CouchDB应该像运行. ~/path/to/build-couchdb/build/env.sh
,然后再couchdb
),将http-console连接到它就像这样:
$> http-console http://127.0.0.1:5984
> http-console 0.6.1
> Welcome, enter .help if you're lost.
> Connecting to 127.0.0.1 on port 5984.
然后,我们可以对数据库发出命令。 让我们获取所有数据库的列表:
http://127.0.0.1:5984/> GET /_all_dbs
HTTP/1.1 200 OK
Server: CouchDB/1.1.1 (Erlang OTP/R15B)
Date: Wed, 04 Jan 2012 08:26:18 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 25
Cache-Control: must-revalidate
[ '_replicator', '_users' ]
如何创建一个新的数据库?
http://127.0.0.1:5984/> PUT /foodb
...
HTTP/1.1 201 Created
Server: CouchDB/1.1.1 (Erlang OTP/R15B)
Location: http://127.0.0.1/foodb
Date: Wed, 04 Jan 2012 09:19:05 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 12
Cache-Control: must-revalidate
{ ok: true }
重新发出GET /_all_dbs
命令,我们将看到列出的新数据库:
http://127.0.0.1:5984/> GET /_all_dbs
HTTP/1.1 200 OK
Server: CouchDB/1.1.1 (Erlang OTP/R15B)
Date: Wed, 04 Jan 2012 09:19:18 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 33
Cache-Control: must-revalidate
[ '_replicator', '_users', 'foodb' ]
现在,将文档添加到foodb
数据库。 我们需要将Content-Type标头设置为application/json
,这很容易通过发出.j
命令来完成(要查看所有可用的命令,请在http-console提示符下键入.help
):
http://127.0.0.1:5984/> .j
http://127.0.0.1:5984/> POST /foodb
... { "name":"foo", "body":"bar" }
HTTP/1.1 201 Created
Server: CouchDB/1.1.1 (Erlang OTP/R15B)
Location: http://127.0.0.1/foodb/d4a833a173e9d22594b426fd300010a9
Date: Wed, 04 Jan 2012 09:36:30 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 95
Cache-Control: must-revalidate
{
ok: true,
id: 'd4a833a173e9d22594b426fd300010a9',
rev: '1-de4f3804f6f3d2d3a393bec924951e5a'
}
我们可以发出HEAD
请求以获取有关文档的信息, DELETE
请求以删除文档以及DELETE
请求以删除数据库:
http://127.0.0.1:5984/> HEAD /foodb/d4a833a173e9d22594b426fd300010a9
HTTP/1.1 200 OK
Server: CouchDB/1.1.1 (Erlang OTP/R15B)
Etag: "1-de4f3804f6f3d2d3a393bec924951e5a"
Date: Wed, 04 Jan 2012 09:36:51 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 113
Cache-Control: must-revalidate
http://127.0.0.1:5984/> DELETE /foodb/d4a833a173e9d22594b426fd300010a9?rev=1-de4f3804f6f3d2d3a393bec924951e5a
HTTP/1.1 200 OK
Server: CouchDB/1.1.1 (Erlang OTP/R15B)
Etag: "2-3ac7397737175948b7a3a6b7e95d2949"
Date: Wed, 04 Jan 2012 09:40:14 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 95
Cache-Control: must-revalidate
{
ok: true,
id: 'd4a833a173e9d22594b426fd300010a9',
rev: '2-3ac7397737175948b7a3a6b7e95d2949'
}
http://127.0.0.1:5984/> DELETE /foodb
HTTP/1.1 200 OK
Server: CouchDB/1.1.1 (Erlang OTP/R15B)
Date: Wed, 04 Jan 2012 09:41:49 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 12
Cache-Control: must-revalidate
{ ok: true }
因此,快速浏览了使用http-console发出和检查HTTP请求的过程。 我们向Web服务器发出了一个简单的GET请求,对Twitter的Search API进行了API调用,并向CouchDB服务器发出了命令。 被授予YMMV,但希望您会发现它对Web开发工具带很有用。
From: https://www.sitepoint.com/http-debugging-with-node-and-http-console/