1 | page = require("webpage").create()
|
---|
2 | server = require("webserver").create()
|
---|
3 | system = require("system")
|
---|
4 | host = undefined
|
---|
5 | port = undefined
|
---|
6 | if system.args.length isnt 2
|
---|
7 | console.log "Usage: server.js <some port>"
|
---|
8 | phantom.exit 1
|
---|
9 | else
|
---|
10 | port = system.args[1]
|
---|
11 | listening = server.listen(port, (request, response) ->
|
---|
12 | console.log "GOT HTTP REQUEST"
|
---|
13 | console.log JSON.stringify(request, null, 4)
|
---|
14 |
|
---|
15 | # we set the headers here
|
---|
16 | response.statusCode = 200
|
---|
17 | response.headers =
|
---|
18 | Cache: "no-cache"
|
---|
19 | "Content-Type": "text/html"
|
---|
20 |
|
---|
21 |
|
---|
22 | # this is also possible:
|
---|
23 | response.setHeader "foo", "bar"
|
---|
24 |
|
---|
25 | # now we write the body
|
---|
26 | # note: the headers above will now be sent implictly
|
---|
27 | response.write "<html><head><title>YES!</title></head>"
|
---|
28 |
|
---|
29 | # note: writeBody can be called multiple times
|
---|
30 | response.write "<body><p>pretty cool :)</body></html>"
|
---|
31 | response.close()
|
---|
32 | )
|
---|
33 | unless listening
|
---|
34 | console.log "could not create web server listening on port " + port
|
---|
35 | phantom.exit()
|
---|
36 | url = "http://localhost:" + port + "/foo/bar.php?asdf=true"
|
---|
37 | console.log "SENDING REQUEST TO:"
|
---|
38 | console.log url
|
---|
39 | page.open url, (status) ->
|
---|
40 | if status isnt "success"
|
---|
41 | console.log "FAIL to load the address"
|
---|
42 | else
|
---|
43 | console.log "GOT REPLY FROM SERVER:"
|
---|
44 | console.log page.content
|
---|
45 | phantom.exit()
|
---|