设为首页 加入收藏

TOP

Node.js入门
2014-11-24 02:31:58 来源: 作者: 【 】 浏览:2
Tags:Node.js 入门

Node.js是什么,这里就不再多说。经过我简单测试,执行相同的任务,Node.js(单进程)比Nginx+php快4倍,当然,这并不表示Node.js的性能就是Nginx+php的4倍,可能不同的任务场景表现各有差异。但这足以让我有兴趣去了解它了。


相关阅读


Node.js可以说就是js,作为php程序员,之前接触的js完全是围绕着浏览器工作的,现在改在服务器上工作了,还有一些不适应的地方,如下:


一,自定义模块


module.js


var cfg = {"info":"Require test."};
module.exports = {
print_hello : function() {
return "Hello,World!";
},
print_info : function() {
return cfg.info;
}
};


main.js


var m = require("./module.js");
console.log( m.print_info() );


二,接收参数


1,get.js


var http = require("http");
var url = require("url");
http.createServer(function(request,response) {
var $get = url.parse(request.url,true).query;
response.writeHead(200,{"Content-Type":"text/plain"});
response.end($get['data']);
}).listen(1337,'192.168.9.10');
console.log("'Server running at http://192.168.9.10:1337/");


2,post.js


var http = require('http');
var querystring = require('querystring');


http.createServer(function (request, response) {
var post_data = '';
request.addListener('data', function(post_chunk) {
post_data += post_chunk;
});


request.addListener('end', function() {
post_data = querystring.parse(post_data);
response.writeHead(200, {'Content-Type':'text/plain'});
response.end(post_data.username);
});
}).listen(1337, '192.168.9.10');
console.log("'Server running at http://192.168.9.10:1337/");




】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇RabbitMQ工作队列实现高性能任务.. 下一篇iOS中多重继承实现的折中方法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: