This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.js
78 lines (62 loc) · 1.5 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var hyperspider = require("../")
var http = require("http")
var assert = require("assert")
var server = http.createServer()
server.on("request", function(req, res) {
var match = req.url.match(/\/users\/(\w+)\/following-detailed/)
var first = true
if (match) {
var crawl = hyperspider({
port: this.address().port,
path: [
"/users/" + match[1],
"/users/" + match[1] + "/following",
"/users/*"
]
})
res.writeHead(200)
res.write("[")
crawl.on("data", function(response) {
first ? first = false : res.write(",")
res.write(response.body)
})
crawl.on("end", function() {
res.end("]")
})
crawl.on("error", function() {
// ignore
})
return
}
try {
var obj = require(__dirname + req.url)
res.writeHead(200)
res.end(JSON.stringify(obj))
}
catch (err) {
var obj = {error: err.message}
res.writeHead(404)
res.end(JSON.stringify(obj))
}
})
server.listen(function() {
var opts = {
port: this.address().port,
path: "/users/jedschmidt/following-detailed"
}
http.get(opts, onResponse)
function onResponse(res) {
var body = ""
res.on("data", function(chunk){ body += chunk })
res.on("end", function() {
var obj
assert.doesNotThrow(function(){ obj = JSON.parse(body) })
assert.ok(Array.isArray(obj))
assert.equal(obj.length, 6)
obj.forEach(function(item) {
assert.ok("href" in item)
})
server.close()
})
}
})