身在網路的世界打滾∼有些事情不得不知道...
在node.js中,他把request做了某個程度的wrapper
我們可以透過下面的方法取得一些常用的連線參數
- 取得server side full url:
req.url - 取得request client的IP位置:
req.connection.remoteAddress - 取得referer page(從哪個網址連線進入)
req.headers['referer'] - 取得user agent information:
req.headers['user-agent']
實際上程式碼可以向這樣:
http.createServer(function (req, res) {
console.log(req.connection);
var result = 'req.url:' + req.url + '\r\n';
result += 'req.connection.remoteAddres:' + req.connection.remoteAddress + '\r\n';
if(req.headers) {
result += 'req.headers[\'referer\']:'+ req.headers['referer'] + '\r\n';
result += 'req.headers[\'user-agent\']:' + req.headers['user-agent'] + '\r\n';
}
res.end(result);
}).listen(port, server);
大家可以參考看看∼
http.createServer(function (req, res) {
console.log(req.connection);
var result = 'req.url:' + req.url + '\r\n';
result += 'req.connection.remoteAddres:' + req.connection.remoteAddress + '\r\n';
if(req.headers) {
result += 'req.headers[\'referer\']:'+ req.headers['referer'] + '\r\n';
result += 'req.headers[\'user-agent\']:' + req.headers['user-agent'] + '\r\n';
}
res.end(result);
}).listen(port, server);
大家可以參考看看∼