BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / phantomjs-prebuilt / lib / phantom / examples / server.js

server.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
server.js 1.5 KiB Download Raw
1"use strict";
2var page = require('webpage').create();
3var server = require('webserver').create();
4var system = require('system');
5var host, port;
6
7if (system.args.length !== 2) {
8 console.log('Usage: server.js <some port>');
9 phantom.exit(1);
10} else {
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));
15
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>");
26 response.close();
27 });
28 if (!listening) {
29 console.log("could not create web server listening on port " + port);
30 phantom.exit();
31 }
32 var url = "http://localhost:" + port + "/foo/bar.php?asdf=true";
33 console.log("SENDING REQUEST TO:");
34 console.log(url);
35 page.open(url, function (status) {
36 if (status !== 'success') {
37 console.log('FAIL to load the address');
38 } else {
39 console.log("GOT REPLY FROM SERVER:");
40 console.log(page.content);
41 }
42 phantom.exit();
43 });
44}