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