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