成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

完全面向于初學(xué)者的Node.js指南

開發(fā) 前端 開發(fā)工具
新的上班時間是周二至周六,工作之余當(dāng)然要堅持學(xué)習(xí)啦。

新的上班時間是周二至周六,工作之余當(dāng)然要堅持學(xué)習(xí)啦。

希望這篇文章能解決你這樣一個問題:“我現(xiàn)在已經(jīng)下載好Node.Js了,該做些什么呢?”

原文URL:http://blog.modulus.io/absolute-beginners-guide-to-nodejs

本文的組成:上文的翻譯以及小部分自己的理解。所有文章中提到的JS代碼,都是經(jīng)過測試,可運行并產(chǎn)生正確結(jié)果的。

What is Node.js?

關(guān)于Node.Js,要注意一點:Node.js本身并不是像IIS,Apache一樣的webserver,它是一個JavaScript 的運行環(huán)境。當(dāng)我們需要搭建一個HTTP 服務(wù)器的時候,我們可以借助Node.Js提供的庫快捷的寫一個。

Installing Node

Node.js 安裝是非常方便的,如果你在用Windows or Mac,去這個頁面就可以了download page.

I've Installed Node, now what?

   以WINDOWS為例,一旦安裝好Node.Js之后,可以通過兩種不同方式來調(diào)用Node。

   方式一:CMD 下輸入node,進(jìn)入交互模式,輸入一行行的JS代碼,Node.Js會執(zhí)行并返回結(jié)果,例子:

  1. $ node 
  2. > console.log('Hello World'); 
  3. Hello World 
  4. undefined 

   PS:上一個例子的undefined來自于console.log的返回值。

    方式二:CMD 下輸入node 文件名(當(dāng)然需要先CD到該目錄)。例子:

 

  1. hello.js 下的代碼: 
  2. console.log('Hello World'); 
  3. $ node hello.js 
  4. Hello World 

 

 

Doing Something Useful - File I/O

    使用純粹的Js原生代碼是有趣但是不利于工程開發(fā)的,Node.JS提供了一些有用的庫(modules),下面是一個使用Node.js提供的庫分析文件的例子:

 

  1. example_log.txt 
  2. 2013-08-09T13:50:33.166Z A 2 
  3. 2013-08-09T13:51:33.166Z B 1 
  4. 2013-08-09T13:52:33.166Z C 6 
  5. 2013-08-09T13:53:33.166Z B 8 
  6. 2013-08-09T13:54:33.166Z B 5 

 

    我們做的***件事情是讀出該文件的所有內(nèi)容。

 

  1. my_parser.js 
  2.  
  3. // Load the fs (filesystem) module 
  4. var fs = require('fs'); 
  5.  
  6. // Read the contents of the file into memory. 
  7. fs.readFile('example_log.txt', function (err, logData) { 
  8.    
  9. // If an error occurred, throwing it will 
  10.   // display the exception and end our app. 
  11.   if (err) throw err; 
  12.    
  13. // logData is a Buffer, convert to string. 
  14.   var text = logData.toString(); 
  15. }); 

 

     filesystem (fs 的API ref) module 提供了一個可以異步讀取文件并且結(jié)束后執(zhí)行回調(diào)的函數(shù),內(nèi)容以 Buffer的形式返回(一個byte數(shù)組),我們可以調(diào)用toString() 函數(shù),將它轉(zhuǎn)換成字符串。

     現(xiàn)在我們再來添加解析部分的代碼。

 

  1. my_parser.js 
  2.  
  3. // Load the fs (filesystem) module. 
  4. var fs = require('fs');//  
  5.  
  6. // Read the contents of the file into memory. 
  7. fs.readFile('example_log.txt', function (err, logData) { 
  8.    
  9. // If an error occurred, throwing it will 
  10.   // display the exception and kill our app. 
  11.   if (err) throw err; 
  12.    
  13. // logData is a Buffer, convert to string. 
  14.   var text = logData.toString(); 
  15.    
  16. var results = {}; 
  17.  
  18. // Break up the file into lines. 
  19.   var lines = text.split('\n'); 
  20.    
  21. lines.forEach(function(line) { 
  22.     var parts = line.split(' '); 
  23.     var letter = parts[1]; 
  24.     var count = parseInt(parts[2]); 
  25.      
  26. if(!results[letter]) { 
  27.       results[letter] = 0
  28.     } 
  29.      
  30. results[letter] += parseInt(count); 
  31.   }); 
  32.    
  33. console.log(results); 
  34.   // { A: 2, B: 14, C: 6 } 
  35. }); 

 

Asynchronous Callbacks

    剛才的例子中使用到了異步回調(diào),這在Node.Js編碼中是廣泛被使用的,究其原因是因為Node.Js是單線程的(可以通過某些特殊手段變?yōu)槎嗑€程,但一般真的不需要這么做)。故而需要各種非阻塞式的操作。

    這種非阻塞式的操作有一個非常大的優(yōu)點:比起每一個請求都創(chuàng)建一個線程的Web Server。Node.Js在高并發(fā)的情況下,負(fù)載是小得多的。

Doing Something Useful - HTTP Server

    我們來運行一個HTTP server吧, 直接復(fù)制 Node.js homepage.上的代碼就可以了。

 

  1. my_web_server.js 
  2.  
  3.     var http = require('http'); 
  4.  
  5.     http.createServer(function (req, res) { 
  6.       res.writeHead(200, {'Content-Type''text/plain'}); 
  7.       res.end('Hello World\n'); 
  8.     }).listen(8080); 
  9.  
  10.     console.log('Server running on port 8080.'); 

 

    運行以上代碼之后就可以訪問http://localhost:8080 就能看到結(jié)果啦。

    上面的例子顯然過于簡單,如果我們需要建立一個真正的web server。我們需要能夠檢查什么正在被請求,渲染合適的文件,并返回。而好消息是,Express已經(jīng)做到這一點了。

Doing Something Useful - Express

    Express 是一個可以簡化開發(fā)的框架。我們執(zhí)行npm install 來安裝這個package。

$ cd /my/app/location
$ npm install express

    指令執(zhí)行完畢后,Express相關(guān)的文件會被放到應(yīng)用目錄下的node_modules文件夾中。下面是一個使用Express開發(fā)的例子:

 

  1. my_static_file_server.js 
  2.  
  3. var express = require('express'), 
  4.     app = express(); 
  5.  
  6.  
  7.  
  8. app.use(express.static(__dirname + '/public')); 
  9.  
  10. app.listen(8080); 
  11.  
  12. $ node my_static_file_server.js 

 

    這樣就建立了一個文件服務(wù)器。入油鍋我們在 /public 文件夾放了一個"my_image.png" 。我們就可以在瀏覽器輸入http://localhost:8080/my_image.png 來獲取這個圖片. 當(dāng)然,Express 還提供了非常多的其它功能。

Code Organization

    剛才的例子中我們使用的都是單個文件,而實際的開發(fā)中,我們會設(shè)計到代碼如何組織的問題。

    我們試著將最開始的文字解析程序重新組織。

 

  1. parser.js 
  2.  
  3. // Parser constructor. 
  4. var Parser = function() { 
  5.  
  6. }; 
  7.  
  8. // Parses the specified text. 
  9. Parser.prototype.parse = function(text) { 
  10.    
  11. var results = {}; 
  12.    
  13. // Break up the file into lines. 
  14.   var lines = text.split('\n'); 
  15.    
  16. lines.forEach(function(line) { 
  17.     var parts = line.split(' '); 
  18.     var letter = parts[1]; 
  19.     var count = parseInt(parts[2]); 
  20.      
  21. if(!results[letter]) { 
  22.       results[letter] = 0
  23.     } 
  24.      
  25. results[letter] += parseInt(count); 
  26.   }); 
  27.    
  28. return results; 
  29. }; 
  30.  
  31. // Export the Parser constructor from this module. 
  32. module.exports = Parser; 

 

   關(guān)于這里的exports 的含義請參考我的博客:Node.Js學(xué)習(xí)01: Module System 以及一些常用Node Module.

  1. my_parser.js 
  2.  
  3. // Require my new parser.js file. 
  4. var Parser = require('./parser'); 
  5.  
  6. // Load the fs (filesystem) module. 
  7. var fs = require('fs'); 
  8.  
  9. // Read the contents of the file into memory. 
  10. fs.readFile('example_log.txt', function (err, logData) { 
  11.    
  12. // If an error occurred, throwing it will 
  13.   // display the exception and kill our app. 
  14.   if (err) throw err; 
  15.    
  16. // logData is a Buffer, convert to string. 
  17.   var text = logData.toString(); 
  18.    
  19. // Create an instance of the Parser object. 
  20.   var parser = new Parser(); 
  21.    
  22. // Call the parse function. 
  23.   console.log(parser.parse(text)); 
  24.   // { A: 2, B: 14, C: 6 } 
  25. }); 

    這樣,文字解析的部分就被抽離了出來。

Summary

    Node.js 是強(qiáng)大而靈活的。

 

 

 
責(zé)任編輯:王雪燕 來源: 博客園
相關(guān)推薦

2010-08-26 15:47:09

vsftpd安裝

2022-04-24 15:21:01

MarkdownHTML

2024-04-28 10:56:34

Next.jsWeb應(yīng)用搜索引擎優(yōu)化

2019-03-29 16:40:02

Node.js多線程前端

2013-12-20 14:47:23

ember.js

2022-07-22 13:14:57

TypeScript指南

2010-06-13 11:13:38

UML初學(xué)者指南

2021-05-10 08:50:32

網(wǎng)絡(luò)管理網(wǎng)絡(luò)網(wǎng)絡(luò)性能

2023-07-28 07:31:52

JavaScriptasyncawait

2022-03-28 09:52:42

JavaScript語言

2023-07-03 15:05:07

預(yù)測分析大數(shù)據(jù)

2022-10-10 15:28:45

負(fù)載均衡

2020-06-03 10:00:30

Kubernetes容器開發(fā)

2018-10-28 16:14:55

Reactreact.js前端

2023-02-10 08:37:28

2012-03-14 10:56:23

web app

2022-09-05 15:36:39

Linux日志記錄syslogd

2024-12-25 08:00:00

機(jī)器學(xué)習(xí)ML管道人工智能

2021-05-06 09:00:00

JavaScript靜態(tài)代碼開發(fā)

2013-04-08 16:35:52

Adobe Edge
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 欧美啊v在线观看 | 亚洲免费在线 | 一级黄色片免费在线观看 | 色婷婷av久久久久久久 | 伊人无码高清 | 国产成人精品午夜视频免费 | 成人国产精品 | 成人免费大片黄在线播放 | 成人综合视频在线观看 | 看av片网站 | 91毛片在线看 | 一级aaaaaa毛片免费同男同女 | 亚洲成人免费av | 99伊人 | 成人性生交大片免费看r链接 | 农村妇女毛片精品久久久 | 欧美xxxⅹ性欧美大片 | 久久精品亚洲欧美日韩久久 | 亚洲成人蜜桃 | 观看av| 99久久久久久 | 午夜小视频在线播放 | 国产精品日产欧美久久久久 | 欧美激情在线一区二区三区 | 91精品国产91久久综合桃花 | 一级欧美视频 | 91视频精选 | 日韩三级在线观看 | 久久久久国产 | 亚洲精品99久久久久久 | 亚洲激情在线观看 | caoporn地址 | 国产一区二区三区四区 | 亚洲一区有码 | 成人在线视频网 | 韩日三级 | 一区二区福利视频 | 欧美性成人 | 99久久婷婷国产综合精品首页 | 伊人中文字幕 | 日韩在线一区二区三区 |