PhantomJS - Web服务器模块方法

在本章中,我们将讨论有关PhantomJS的Web服务器模块的各种方法.

close

关闭方法用于关闭网络服务器.

语法

其语法如下 :

 
 var server = require('webserver').create(); 
 server.close();

示例

以下示例说明如何使用关闭方法.

var webserver = require('webserver');
var server = webserver.create();
var service = server.listen(8080,function(request,response){
});

if(service) console.log("server started - http://localhost:" + server.port);
console.log(server.port);
server.close();
console.log(server.port);

上述程序生成以下输出.

server started - http://localhost:8080 
8080

此处,我们已安慰 server.port 关闭服务器后.因此,它不会响应,因为网络服务器已经关闭.

listen

server.listen 方法需要具有两个参数的端口和回调函数,它们是 -  请求对象响应对象.

请求对象包含以下属性 :

  • 方法 : 这定义了方法GET/POST.

  • URL : 这会显示请求的网址.

  • httpVersion : 这会显示实际的http版本.

  • 标题 : 这会显示所有带键值对的标题.

  • 发布 : 请求正文仅适用于发布方法.

  • postRaw : 如果Content-Type标头设置为'application/x-www-formurlencoded',则帖子的原始内容将存储在此额外属性(postRaw)中,然后该帖子将自动更新为URL解码版本数据.

响应对象包含以下属性 :

  • 标题 : 将所有HTTP标头都作为键值对.它应该在第一次调用write之前设置.

  • SetHeader : 这会设置一个特定的标题.

  • 标题(名称) : 它返回给定标题的值.

  • StatusCode : 它设置返回的HTTP状态代码.

  • SetEncoding(encoding) : 这用于转换给write()的数据.默认情况下,数据将转换为UTF-8.如果数据是二进制字符串,则指示"二进制".如果数据是缓冲区(例如来自page.renderBuffer),则不需要.

  • 写入(数据) : 它为响应主体发送数据.可以多次调用.

  • WriteHead(statusCode,headers) : 它向请求发送响应头.状态代码是3位HTTP状态代码(例如404).最后一个参数和标题是响应标题.

  • 关闭 : 它会关闭http连接.

  • CloseGracefully : 它类似于close(),但它确保首先发送响应头.

语法

其语法如下 :

var server = require('webserver').create(); 
var listening = server.listen(8080, function (request, response) {}

示例

让我们举个例子来理解 listen 方法是如何工作的.

var page = require('webpage').create(); 
var server = require('webserver').create(); 
var port = 8080; 
var listening = server.listen(8080, function (request, response) { 
   console.log("GOT HTTP REQUEST"); 
   console.log(JSON.stringify(request, null, 4)); 
   
   // we set the headers here 
   response.statusCode = 200; 
   response.headers = {"Cache": "no-cache", "Content-Type": "text/html"};  
 
   // the headers above will now be sent implictly 
   // now we write the body 
   response.write("<html><head><title>Welcone to Phantomjs</title></head>"); 
   response.write("<body><p>Hello World</p></body></html>"); 
   response.close(); 
}); 

if (!listening) { 
   console.log("could not create web server listening on port " + port); 
   phantom.exit(); 
} 

var url = "https://img01.yuandaxia.cn/Content/img/tutorials/phantomjs/" + port + "/foo/response.php"; 
console.log("sending request to :" +url); 
page.open(url, function (status) { 
   if (status !== 'success') { 
      console.log('page not opening'); 
   } else { 
      console.log("Getting response from the server:"); 
      console.log(page.content); 
   } 
   
   phantom.exit(); 
});

上述程序生成以下输出.

sending request to :http://localhost:8080/foo/response.php 
GOT HTTP REQUEST { 
   "headers": {
      "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
      "Accept-Encoding": "gzip, deflate", 
      "Accept-Language": "en-IN,*", 
      "Connection": "Keep-Alive", 
      "Host": "localhost:8080", 
      "User-Agent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 
         (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1" 
   }, 
   "httpVersion": "1.1", 
   "method": "GET", 
   "url": "/foo/response.php" 
} 
Getting response from the server: 
<html><head><title>Welcone to Phantomjs</title></head><body><p>Hello World</p></body>
</html>