下面將會展示一個簡單的轉換示例。
假設現在你有一個簡單的 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。