自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

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

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

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

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

What is Node.js?

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

Installing Node

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

I've Installed Node, now what?

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

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

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

   PS:上一個(gè)例子的undefined來(lái)自于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原生代碼是有趣但是不利于工程開(kāi)發(fā)的,Node.JS提供了一些有用的庫(kù)(modules),下面是一個(gè)使用Node.js提供的庫(kù)分析文件的例子:

 

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

     現(xiàn)在我們?cè)賮?lá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 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編碼中是廣泛被使用的,究其原因是因?yàn)镹ode.Js是單線程的(可以通過(guò)某些特殊手段變?yōu)槎嗑€程,但一般真的不需要這么做)。故而需要各種非阻塞式的操作。

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

Doing Something Useful - HTTP Server

    我們來(lái)運(yùn)行一個(gè)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.'); 

 

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

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

Doing Something Useful - Express

    Express 是一個(gè)可以簡(jiǎn)化開(kāi)發(fā)的框架。我們執(zhí)行npm install 來(lái)安裝這個(gè)package。

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

    指令執(zhí)行完畢后,Express相關(guān)的文件會(huì)被放到應(yīng)用目錄下的node_modules文件夾中。下面是一個(gè)使用Express開(kāi)發(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 

 

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

Code Organization

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

    我們?cè)囍鴮⒆铋_(kāi)始的文字解析程序重新組織。

 

  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 的含義請(qǐng)參考我的博客: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. }); 

    這樣,文字解析的部分就被抽離了出來(lái)。

Summary

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

 

 

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

2010-08-26 15:47:09

vsftpd安裝

2024-04-28 10:56:34

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

2022-04-24 15:21:01

MarkdownHTML

2019-03-29 16:40:02

Node.js多線程前端

2013-12-20 14:47:23

ember.js

2022-10-10 15:28:45

負(fù)載均衡

2010-06-13 11:13:38

UML初學(xué)者指南

2022-07-22 13:14:57

TypeScript指南

2021-05-10 08:50:32

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

2022-03-28 09:52:42

JavaScript語(yǔ)言

2023-07-28 07:31:52

JavaScriptasyncawait

2023-07-03 15:05:07

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

2020-06-03 10:00:30

Kubernetes容器開(kāi)發(fā)

2018-10-28 16:14:55

Reactreact.js前端

2012-03-14 10:56:23

web app

2023-02-10 08:37:28

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)代碼開(kāi)發(fā)

2013-03-06 10:40:58

Adobe Edge HTML5
點(diǎn)贊
收藏

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