node.js小結 2

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   問題   

下載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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.