Skip to content

Commit 11e4750

Browse files
committed
Get a page title from URL
1 parent da43454 commit 11e4750

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

24-express-nowjs/server.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
var express=require('express'), app = express.createServer();
42
app.listen(8000);
53

29-http-get-page-title.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
console.log('http://nodejs.org/docs/v0.4.5/api/http.html#http.request');
2+
3+
var http=require('http'), url=require('url');
4+
var u=url.parse(process.argv.length>2?process.argv[2]:'http://www.delicious.com/straps');
5+
6+
console.dir(u);
7+
8+
var options = {
9+
host: u.host,
10+
port: u.port||80,
11+
path: u.pathname||'/'
12+
};
13+
14+
console.dir(options);
15+
16+
http.get(options, function(res) {
17+
var body='', title='';
18+
res.on('data', function (chunk) {
19+
body+=chunk;
20+
if (!title && /<title>.*<\/title>/im.test(body)){
21+
console.log('data arrived, testing...');
22+
title=body.match(/<title>(.*)<\/title>/im)[1];
23+
}
24+
});
25+
res.on('end', function(){
26+
console.log('title: '+unescape(title));
27+
});
28+
}).on('error', function(e) {
29+
console.log("Got error: " + e.message);
30+
});

0 commit comments

Comments
 (0)