2015-04-10 10:51:00 +03:00
|
|
|
"use strict";
|
2012-01-14 19:13:26 +04:00
|
|
|
var port, server, service,
|
|
|
|
system = require('system');
|
2011-12-19 20:27:00 +04:00
|
|
|
|
2012-01-14 19:13:26 +04:00
|
|
|
if (system.args.length !== 2) {
|
2011-12-19 20:27:00 +04:00
|
|
|
console.log('Usage: simpleserver.js <portnumber>');
|
2012-05-16 11:37:26 +04:00
|
|
|
phantom.exit(1);
|
2011-12-19 20:27:00 +04:00
|
|
|
} else {
|
2012-01-14 19:13:26 +04:00
|
|
|
port = system.args[1];
|
2011-12-19 20:27:00 +04:00
|
|
|
server = require('webserver').create();
|
|
|
|
|
|
|
|
service = server.listen(port, function (request, response) {
|
|
|
|
|
|
|
|
console.log('Request at ' + new Date());
|
2011-12-20 06:18:08 +04:00
|
|
|
console.log(JSON.stringify(request, null, 4));
|
2011-12-19 20:27:00 +04:00
|
|
|
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.headers = {
|
|
|
|
'Cache': 'no-cache',
|
|
|
|
'Content-Type': 'text/html'
|
|
|
|
};
|
2011-12-20 06:18:08 +04:00
|
|
|
response.write('<html>');
|
|
|
|
response.write('<head>');
|
|
|
|
response.write('<title>Hello, world!</title>');
|
|
|
|
response.write('</head>');
|
|
|
|
response.write('<body>');
|
|
|
|
response.write('<p>This is from PhantomJS web server.</p>');
|
|
|
|
response.write('<p>Request data:</p>');
|
|
|
|
response.write('<pre>');
|
|
|
|
response.write(JSON.stringify(request, null, 4));
|
|
|
|
response.write('</pre>');
|
|
|
|
response.write('</body>');
|
|
|
|
response.write('</html>');
|
2012-02-10 22:42:38 +04:00
|
|
|
response.close();
|
2011-12-19 20:27:00 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (service) {
|
|
|
|
console.log('Web server running on port ' + port);
|
|
|
|
} else {
|
|
|
|
console.log('Error: Could not create web server listening on port ' + port);
|
|
|
|
phantom.exit();
|
|
|
|
}
|
|
|
|
}
|