日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
完全面向于初學(xué)者的Node.js指南

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

創(chuàng)新互聯(lián)建站是一家以網(wǎng)絡(luò)技術(shù)公司,為中小企業(yè)提供網(wǎng)站維護、網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、網(wǎng)站備案、服務(wù)器租用、域名注冊、軟件開發(fā)、重慶小程序開發(fā)等企業(yè)互聯(lián)網(wǎng)相關(guān)業(yè)務(wù),是一家有著豐富的互聯(lián)網(wǎng)運營推廣經(jīng)驗的科技公司,有著多年的網(wǎng)站建站經(jīng)驗,致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開一個面向全國乃至全球的業(yè)務(wù)窗口:建站咨詢電話:028-86922220

希望這篇文章能解決你這樣一個問題:“我現(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)境。當我們需要搭建一個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,進入交互模式,輸入一行行的JS代碼,Node.Js會執(zhí)行并返回結(jié)果,例子:

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

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

    方式二:CMD 下輸入node 文件名(當然需要先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. // Load the fs (filesystem) module
  3. var fs = require('fs');
  4. // Read the contents of the file into memory.
  5. fs.readFile('example_log.txt', function (err, logData) {
  6.   
  7. // If an error occurred, throwing it will
  8.   // display the exception and end our app.
  9.   if (err) throw err;
  10.   
  11. // logData is a Buffer, convert to string.
  12.   var text = logData.toString();
  13. });

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

Asynchronous Callbacks

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

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

Doing Something Useful - HTTP Server

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

   
  
  1. my_web_server.js
  2.     var http = require('http');
  3.     http.createServer(function (req, res) {
  4.       res.writeHead(200, {'Content-Type': 'text/plain'});
  5.       res.end('Hello World\n');
  6.     }).listen(8080);
  7.     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. var express = require('express'),
  3.     app = express();
  4. app.use(express.static(__dirname + '/public'));
  5. app.listen(8080);
  6. $ node my_static_file_server.js

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

Code Organization

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

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

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

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

  
 
  1. my_parser.js
  2. // Require my new parser.js file.
  3. var Parser = require('./parser');
  4. // Load the fs (filesystem) module.
  5. var fs = require('fs');
  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. // Create an instance of the Parser object.
  17.   var parser = new Parser();
  18.   
  19. // Call the parse function.
  20.   console.log(parser.parse(text));
  21.   // { A: 2, B: 14, C: 6 }
  22. });

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

Summary

    Node.js 是強大而靈活的。


當前題目:完全面向于初學(xué)者的Node.js指南
文章網(wǎng)址:http://www.dlmjj.cn/article/cdcjdjg.html