根据HTTP协议规范9.4 HEAD
HEAD 方法与 GET 相同
除了服务器不能返回
响应中的消息正文。这
HTTP 中包含的元信息
响应 HEAD 请求的标头
应该与信息相同
为响应 GET 请求而发送。
该方法可用于获取
关于实体的元信息
由请求暗示而没有
转移实体主体本身。
这种方法常用于测试
有效性的超文本链接,
可访问性和最近
修改。
对 HEAD 请求的响应可能是
在某种意义上是可缓存的
响应中包含的信息
可用于更新以前的
来自该资源的缓存实体。如果
新的字段值表明
缓存的实体与当前的不同
实体(如将由
Content-Length、Content-MD5、
ETag 或 Last-Modified),然后是缓存
必须将缓存条目视为陈旧的。
如果您的服务器对此没有正确响应,我认为您可能不走运?
接下来只需使用google.request('HEAD' 而不是google.request('GET'
一些代码
我在下面测试了以下内容。
fake.js 只是一个使用 express 测试的假服务器。
fake.js:
var HOST = 'localhost';
var PORT = 3000;
var connections = 0;
var express = require('express');
var app = module.exports = express.createServer();
if (process.argv[2] && process.argv[3]) {
HOST = process.argv[2];
PORT = process.argv[3];
}
app.use(express.staticProvider(__dirname + '/public'));
// to reconnect.
app.get('/small', function(req, res) {
console.log(req.method);
if (req.method == 'HEAD') {
console.log('here');
res.send('');
} else {
connections++;
res.send('small');
}
});
app.get('/count', function(req, res) {
res.send('' + connections);
});
app.get('/reset', function(req, res) {
connections = 0;
res.send('reset');
});
if (!module.parent) {
app.listen(PORT, HOST);
console.log("Express server listening on port %d", app.address().port)
}
test.js 是从 http-client 测试 head。
test.js:
var http = require('http');
var google = http.createClient(3000, 'localhost');
var request = google.request('HEAD', '/small',
{'host': 'localhost'});
request.end();
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
});
alfred@alfred-laptop:~/node/stackoverflow/4832362$ curl http://localhost:3000/count
0
alfred@alfred-laptop:~/node/stackoverflow/4832362$ node test.js
STATUS: 200
HEADERS: {"content-type":"text/html; charset=utf-8","content-length":"0","connection":"close"}
alfred@alfred-laptop:~/node/stackoverflow/4832362$ curl http://localhost:3000/count
0
如您所见,仍为 0。