下面将会展示一个简单的转换示例。
假设现在你有一个简单的 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。