Path: blob/master/6-Selenium/phantomjs/examples/server.js
164 views
var page = require('webpage').create();1var server = require('webserver').create();2var system = require('system');3var host, port;45if (system.args.length !== 2) {6console.log('Usage: server.js <some port>');7phantom.exit(1);8} else {9port = system.args[1];10var listening = server.listen(port, function (request, response) {11console.log("GOT HTTP REQUEST");12console.log(JSON.stringify(request, null, 4));1314// we set the headers here15response.statusCode = 200;16response.headers = {"Cache": "no-cache", "Content-Type": "text/html"};17// this is also possible:18response.setHeader("foo", "bar");19// now we write the body20// note: the headers above will now be sent implictly21response.write("<html><head><title>YES!</title></head>");22// note: writeBody can be called multiple times23response.write("<body><p>pretty cool :)</body></html>");24response.close();25});26if (!listening) {27console.log("could not create web server listening on port " + port);28phantom.exit();29}30var url = "http://localhost:" + port + "/foo/bar.php?asdf=true";31console.log("SENDING REQUEST TO:");32console.log(url);33page.open(url, function (status) {34if (status !== 'success') {35console.log('FAIL to load the address');36} else {37console.log("GOT REPLY FROM SERVER:");38console.log(page.content);39}40phantom.exit();41});42}434445