node.js小结 2

下载node安装npm什么的就不说了

 

 

入门总结

http://www.cnblogs.com/Darren_code/archive/2011/10/31/nodejs.html

 

进入node_HOME目录

test.js

/*var hello = require('./hello.js');
console.log('hello.location : ' + hello.location)
console.log('hello.location : ' + hello.name)
hello.showLog();*/

    var fs = require('fs');
  console.log('11');
    var hello = require('./hello.js');
  console.log('22');

  fs.readFile('./hello.js', function (err, data) {
    if (err) throw err;
    console.log('successfully');
  });
  console.log('async');

var events = require("events"); 
  var emitter = new events.EventEmitter(); 
  emitter.on("myEvent", function(msg) { 
    console.log(msg); 
  }); 
  emitter.emit("myEvent", "Hello World.");


var fs = require('fs');
  fs.unlink('./hello.js', function (err) {
    if (err) throw err;
    console.log('successfully deleted myModule.js');
  });

 

hello.js

  console.log('00');
      var name = "Darren";
  this.location = "Beijing";
  this.showLog = function(){
      console.log('Hi Darren.')
  };

 

输出:

11
00
22
async
Hello World.
successfully deleted myModule.js
successfully

 

基本概念的解析:

模块:其实就相当于一个js类, 通过require(路径)方式创建并引入一个模块

异步:

 

node各内置模块及其api

sys

fs

http

agent

url

path

file

events 

 

 

 

 http://nodejs.iteye.com/blog/1587019

通常, js 通过eval 组装 服务端传来的 json字符串成 js对象 -- 服务端有另外的json解释器

var aa = JSON.parse("{\"a\":\"c\"}");   JSON 也是js内置的

 

 

服务端对浏览器的请求处理通常是: 每个请求建立一个线程。。。? 

 

 

一定要明白你需要解决的问题是什么,基于此采取最佳解决方案,而不是基于你当下所掌握的技能来解决遇到的问题

 

node的优势:

无阻塞(异步)、

高并发(异步、基于事件的i/o)

构建可伸缩的网络应用(单线程_ 传统web服务器的一个请求一个线程,即多线程)

 

 

 

 

非常棒的文章:

http://www.cnblogs.com/liusuqi/p/3735491.html

 

posted on 2014-07-20 01:10 CanntBelieve 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/FlyAway2013/p/3813576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值