00b462aaa83afbbe11e38fabf06ebea6cdc89e2c
[yaffs-website] / node_modules / phantomjs-prebuilt / lib / phantom / examples / serverkeepalive.js
1 "use strict";
2 var port, server, service,
3     system = require('system');
4
5 if (system.args.length !== 2) {
6     console.log('Usage: serverkeepalive.js <portnumber>');
7     phantom.exit(1);
8 } else {
9     port = system.args[1];
10     server = require('webserver').create();
11
12     service = server.listen(port, { keepAlive: true }, function (request, response) {
13         console.log('Request at ' + new Date());
14         console.log(JSON.stringify(request, null, 4));
15
16         var body = JSON.stringify(request, null, 4);
17         response.statusCode = 200;
18         response.headers = {
19             'Cache': 'no-cache',
20             'Content-Type': 'text/plain',
21             'Connection': 'Keep-Alive',
22             'Keep-Alive': 'timeout=5, max=100',
23             'Content-Length': body.length
24         };
25         response.write(body);
26         response.close();
27     });
28
29     if (service) {
30         console.log('Web server running on port ' + port);
31     } else {
32         console.log('Error: Could not create web server listening on port ' + port);
33         phantom.exit();
34     }
35 }