您的位置:首页 > Web前端 > Node.js

node.js初探-超越昨天的自己系列(3)

2013-06-02 16:16 267 查看

超越昨天的自己系列(3)

  其实,学习这事总是被动的,有一天,当我们明白需要努力学习的时候,才发现,知识的世界是那么的浩淼,见不到岸,甚至见不到日出,迷雾重重,困惑的我们很容易甩一甩手不想继续了。所以说:进步从来都不是件容易的事情。

------------------------------------------

node.js 接触接触,学习点新东西,对未来的自己总是好的。

从这里开始:http://www.nodejs.org/

1,require 问题

如下代码:

http = require("http");


http作为module(模块)导入进来,这样就可以使用它的方法了。类似java里的import。
javascript本身不支持module,如此就出现了所谓的CommonJS

CommonJS(http://www.commonjs.org)规范的出现,其目标是为了构建JavaScript在包括Web服务器,桌面,命令行工具,及浏览器方面的生态系统。

CommonJS制定了解决这些问题的一些规范,而Node.js就是这些规范的一种实现。Node.js自身实现了require方法作为其引入 模块的方法,同时NPM也基于CommonJS定义的包规范,实现了依赖管理和模块自动安装等功能。

基本的写法如下:

1.modulea.js

module.exports.add = function add(n,m){
var count = n + m;
console.log(n + "+" + m + "=" + count);
return count;
};


2.moduleb.js

a = require("./modulea");
a.add(1,2);


比较具体的例子可以参考这文章:

http://openmymind.net/2012/2/3/Node-Require-and-Exports/

2,关于回调函数

在node.js的编码中大量使用了函数回调,先熟悉一下:


实例一

function invoke_and_add(a,b){

return a()+b();

}

function one(){

return 1;

}

function two(){

return 2;

}

invoke_and_add(one ,two);

结果为3;

实例二 匿名函数

invoke_and_add(function(){return 1;},function(){return 2;})

我们用匿名函数替代了 one,two两个函数。

通过上面两个实例,回调函数的定义为:传递一个函数A到另一个函数B中,并且这个函数B执行函数A。我们就说函数A叫做回调函数。如果没有名称,就叫做匿名回调函数

至于回调和异步的关系,可以参考这篇文章:

http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF%BC%BFjavascript.html

3,hello world


用node.js构建一个服务器,简单到发指,入门文章中都有提到。

var http = require("http");

http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);


监听8888端口,任何请求都会打印Hello World。

4,关于实现一个调用baidu地图的api

通过坐标,请求具体地址,就是发送个http请求而已,连解析返回值都没做,呵呵

http = require("http");
var key="37492c0ee6f924cb5e934fa08c6b1676";
var location="39.983424,%20116.322987";
var output="json";
var path = "/geocoder?location=" + location + "&output=" + output + "&key=" + key
var resultJson="";

options={
host:"api.map.baidu.com",
method:"get",
path:path
};
//http://api.map.baidu.com/geocoder?output=json&location=39.983424,%20116.322987&key=37492c0ee6f924cb5e934fa08c6b1676
//http://api.map.baidu.com/geocoder?location=纬度,经度&output=输出格式类型&key=用户密钥
req = http.request(options, function(res){
//console.log('STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers));
// 取body内容
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
resultJson = resultJson + chunk;
});
console.log('resultJson: ' + resultJson);
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});

req.end();
//返回结果:
/**
* resultJson:
BODY: {
"status":"OK",
"result":{
"location":{
"lng":116.322987,
"lat":39.983424
},
"formatted_address":"北京市海淀区中关村大街27号1101-08室",
"business":"人民大学,中关村,苏州街",
"addressComponent":{
"city":"北京市",
"district":"海淀区",
"province":"北京市",
"street":"中关村大街",
"street_number":"27号1101-08室"
},
"cityCode":131
}
}
**/


--------------------------------------------------

让我们继续前行!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: