This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
API Reference WebServer
laurentj edited this page Feb 5, 2013
·
7 revisions
This is a living document. As the codebase is updated, we hope to keep this document updated as well. Unless otherwise stated, this document currently applies to the latest PhantomJS release: PhantomJS 1.8.0
Note: This page serves as a reference. To learn step-by-step on how to use PhantomJS, please refer to the Quick Start guide.
## Module: WebServer ## **Stability:** _EXPERIMENTAL_ **Introduced:** PhantomJS 1.4 Using an embedded web server module called [Mongoose](http://code.google.com/p/mongoose/), PhantomJS script can start a web server. This is intended for ease of communication between PhantomJS scripts and the outside world and is _not_ recommended for use as a general production server.Here is a simple example that always gives the same response regardless of the request:
var server = require('webserver').create();
var service = server.listen(8080, function(request, response) {
response.statusCode = 200;
response.write('<html><body>Hello!</body></html>');
response.close();
});
If you want to bind to specify address, just use ipaddress:port
instead of port
.
Example:
var server = require('webserver').create();
var service = server.listen('127.0.0.1:8080', function(request, response) {
response.statusCode = 200;
response.write('<html><body>Hello!</body></html>');
response.close();
});
The value returned by server.listen()
is a boolean: true if the server is launched.
The server
object contains these functions:
-
close()
: close the server -
port
: the port on which the server listen requests (readonly)