第二章簡單的Node.js框架——2.3 一個PHP示例頁面

字號+ 編輯: Snake 修訂: 小红帽 來源: 《写给PHP开发者的Node.js学习指南》 2023-09-11 我要說兩句(1)

page()函數就是頁面本身。從廣義上講,這個過程就是將 PHP 文件中的 PHP 代碼複制到page()函數中。然後將page()函數中的PHP 代碼轉換成Node.js 代碼。當page() 函數中只有 Node.js 代碼並且複制過來的 PHP 代碼一點不剩的時候,page()函數就會和 PHP 代碼的行爲完全相同,除了它是 Node.js 而不是 PHP。

下面将会展示一个简单的转换示例。

假设现在你有一个简单的 showx5.php 页面,它同时包含 PHP 和HTML:

<?php
  $x = $_REQUEST['x'];
  $x += 5;
?>
<html><head></head><body>
The value of x plus 5 is <?php echo $x; ?>.
</body></html>

首先,把 PHP 代码拷贝并粘贴到 page()函数中,这会产生比较奇怪的,没有功能的 PHP/Node.js 混合代码:

function page(req,res,pre,cb){
  var content = '';
  <?php
    $x = $_REQUEST['x'];
    $x += 5;
  ?>
  <html><head></head><body>
  The value of x plus 5 is <?php echo $x; ?>.
  </body></html>
  res.writeHead(200,{'Content-Type':'text/html'});
  res.end(content);
  cb();
}

接下来,转换第一个 PHP 代码模块。这里是转换前的片段:

<?php
  $x = $_REQUEST['x'];
  $x += 5;
?>

转换之后,它看起来仍然非常简单,除了现在它已经是 Node.js 代码了:

var x = parseInt(pre._REQUEST['x']);
x += 5;

然后,剩下的 HTML 代码也会被转换:

<html><head></head><body>
  The value of x plus 5 is <?php echo $x; ?>.
  </body></html>

它对应的 Node.js 代码页非常简单:

content += '<htnl><head></head><body>';
content += 'The value of x plus 5 is '+x+'.';
content += '</body></html>';

下面是 showx5.njs 中完全转换好的 page()函数,它实现了 showx5.php 一样的功能:

function page(req,res,pre,cb){
  var content ='';
  var x = parseInt(pre._RESUEST['x']);
  x += 5;
  content += '<htnl><head></head><body>';
  content += 'The value of x plus 5 is '+x+'.';
  content += '</body></html>'; 
  res.writeHead(200,{'Content-Type':'text/html'});
  res.end(content);
  cb();
}

下面是完整的 showx5.njs 文件:

var initreq = require('./initreq.njs');
exports.serve = function(req,res){
  var pre = {};
  initreq.initGET(req,pre,function(){
    initreq.initPOST(req,pre,function(){
      initreq.initCOOKIE(req,pre,function(){
        initreq.initGREQUEST(req,pre,function(){
          initreq.initSESSION(req,pre,function(){
            page(req,pre,function(){
              var cookies = [];
              for (var c in pre._COOKIE){
                cookies.push(c + '=' + pre._COOKIE[c]);
              }
              res.setHeader('Set-Cookie',cookies);
              res.writeHead(200,{'Content-
Type':'text/html'});
              res.end(res.content);
            });
          });
        });
      });
    });
  });
};
function page(req,res,pre,cb){
  var content ='';
  var x = parseInt(pre._RESUEST['x']);
  x += 5;
  content += '<htnl><head></head><body>';
  content += 'The value of x plus 5 is '+x+'.';
  content += '</body></html>'; 
  res.writeHead(200,{'Content-Type':'text/html'});
  res.end(content);
  cb();
}

然后修改 httpsvr.njs 文件,将 showx5.php URL 指定到 showx5.njs 本地模块:

var http = require('http');
var static = require('node-static');
var file = new static.Server();
var url = require('url');
var showx5 = require('./showx5.njs');
http.createServer(cunction(req,res){
  if(url.parse(req,res).pathname == '/showx5.php'){
    showx5.serve(req,res);
  } else{
    file.serve(req,res);
  }
}).listen(1337,'127.0.0.1');
console.log('Server running at htto://127.0.0.1:1337/');

假如你将 httpsvr.njs、initreq.njs 和 showx5.njs 文件放到同一个目录下并且运行Node.js 服务器,那么 PHP 和 Node.js 代码执行效果都一样。使用客户端如浏览器去访问下面的 URL 会得到一样的结果:

http://localhost/showx5.php?x=22
http://localhost:1337/showx5.php?x=22

第一个 URL 会请求 PHP 服务器。第二个 URL 会请求与之相同的 Node.js Web服务器。

虽然 showx5 示例是微不足道的,但是它演示了三件事情:

你有一个用来将 PHP 转换到 Node.js 的开发环境。

你有一个 Node.js 框架来支持每一个 PHP 页面到 Node.js 的转换。

你可以很容易地把琐碎的 PHP 页面转换成 Node.js。

在可以将琐碎的 PHP 页面转换成 Node.js 后,本书剩下章节将关注于如何转换那些复杂的,真实世界中的 PHP 页面。在下一章中,我们将讨论回调函数以及代码线性的概念,将会展示如何在将 PHP 代码粘贴到 page()函数之前进行重构以利于之后更容易地转换成 Node.js。

閲完此文,您的感想如何?
  • 有用

    1

  • 沒用

    1

  • 開心

    1

  • 憤怒

    1

  • 可憐

    1

1.如文章侵犯了您的版權,請發郵件通知本站,該文章將在24小時内刪除;
2.本站標注原創的文章,轉發時煩請注明來源;
3.交流群: 2702237 13835667

相關課文
  • JS如何防止父節點的事件運行

  • nodejs編寫一個簡單的http請求客戶耑代碼demo

  • 說一則爲什麽後耑開發人員不選擇node.js的原因

  • 使用Sublime Text3 開發React-Native的配置

我要說說
網上賓友點評
1 樓 IP 103.216.***.5 的嘉賓 高兴地说 : 很久前
6666666666666666