上一篇文章,我們通過一個簡單的例子,學習了NodeJS中對客戶端的請求(request)對象的解析和處理,整個文件共享的功能已經完成。但是,縱觀整個過程,還有兩個地方明顯需要改進:
首先,不能共享完畢之后,都通過Ctrl+C來關閉NodeJS服務器。
其次,如果僅僅能向客戶端提供d:\ilinkit_logo.png文件的下載,是沒有意義的,共享哪個文件,應該可以通過傳入的參數來指定。
老規矩,先上一個圖:
我們首先來實現退出功能,如果客戶端向服務器提交了http://localhost:8000/exit的請求,我們就執行服務器的退出操作。
上一篇文章我們已經能夠識別出/xiaohong的請求,所以這個實現起來很簡單,代碼如下:
1 var http = require( 'http' ); 2 var fs = require('fs'); 3 var url = require( 'url' ); 4 5 var file_path = "D:\\ilinkit_logo.png" ; 6 var file_stream ; 7 var buffer_box = [] ; 8 var file_length = 0 ; 9 10 var file_name = file_path.substr( file_path.lastIndexOf('\\')+1 ); 11 12 fs.stat( file_path , function ( err , stat ){ 13 if (err) { 14 if ('ENOENT' == err.code) { 15 console.log( 'File does not exist...' ); 16 } else { 17 console.log( 'Read file exception...' ); 18 } 19 } else { 20 file_stream = fs.createReadStream( file_path ); 21 file_stream.on( 'data' , function( chunk ){ 22 buffer_box.push( chunk ) ; 23 file_length += chunk.length ; 24 } ); 25 file_stream.on( 'end' , function( ){ 26 console.log( "文件讀取完畢" ); 27 } ); 28 file_stream.on('error', function(err){ 29 console.log( "文件讀取失敗!" ); 30 }); 31 32 var server =http.createServer( function ( request ,response ){ 33 var h_name = request.headers.host ; 34 var h_path = url.parse( request.url ).pathname ; 35 36 if( h_path === '/xiaohong' ){ 37 response.setHeader( 'Content-Type' , 'application/octet-stream' ); 38 response.setHeader( 'Content-Disposition' , 'attachment; filename=' + encodeURIComponent(file_name) ); 39 40 for( var buffer_index = 0 ; buffer_index<buffer_box.length ; buffer_index++ ) 41 { 42 response.write( buffer_box[buffer_index] ); 43 } 44 response.end(); 45 } 46 else if( h_name === 'localhost:8000' && h_path === '/exit' ){ 47 response.end('Bye!'); 48 server.close() ; 49 console.log( 'Bye!' ); 50 } 51 else{ 52 response.end( 'Hello, iLinkIT' ); 53 } 54 55 } ); 56 server.listen( 8000 ); 57 console.log( 'HTTP服務器啟動中,端口:8000.....' ); 58 59 }//end else,讀取文件沒有發生錯誤 60 });
關鍵的代碼解析如下:
第33行,我們通過request對象獲取客戶端請求的主機及端口內容。
第46行~第50行,我們判斷客戶端提交的請求信息,如果是http://localhost:8000/exit,則調用server.close()關閉服務器。為什么要判斷是不是localhost提交的請求?因為我們希望僅僅在服務器本地提交的請求才能關閉NodeJS服務器。
驗證方式如下:
1. 啟動服務器:打開命令行,進入js腳本所在的位置,執行:node h_ilinkit_1.js。
2. 打開瀏覽器,輸入:http://localhost:8000,顯示如下:
說明當前服務器啟動正常。
3. 打開瀏覽器,輸入:http://localhost:8000/exit。
提示NodeJS服務器已經關閉,我們把瀏覽器關閉之后,發現服務器已經正常退出,如下所示。
這樣,我們就沒必要每次為了退出服務器,都去按Ctrl + C了。
通過請求退出服務器就介紹到這里,接下來我們再看一下,如果在啟動NodeJS服務器的時候,給它傳入參數。對應到我們愛蓮(iLinkIT)的場景,希望能夠將要共享的文件的路徑作為參數傳遞給NodeJS服務器,服務器根據傳入的文件路徑讀取數據到緩沖區,接受客戶端的響應。
代碼如下:
1 var http = require( 'http' ); 2 var fs = require('fs'); 3 var url = require( 'url' ); 4 5 var args = process.argv.splice( 2 ); 6 var file_path = args.join( '' ) ; 7 var file_stream ; 8 var buffer_box = [] ; 9 var file_length = 0 ; 10 11 var file_name = file_path.substr( file_path.lastIndexOf('\\')+1 ); 12 13 fs.stat( file_path , function ( err , stat ){ 14 if (err) { 15 if ('ENOENT' == err.code) { 16 console.log( 'File does not exist...' ); 17 } else { 18 console.log( 'Read file exception...' ); 19 } 20 } else { 21 file_stream = fs.createReadStream( file_path ); 22 file_stream.on( 'data' , function( chunk ){ 23 buffer_box.push( chunk ) ; 24 file_length += chunk.length ; 25 } ); 26 file_stream.on( 'end' , function( ){ 27 console.log( "文件讀取完畢" ); 28 } ); 29 file_stream.on('error', function(err){ 30 console.log( "文件讀取失敗!" ); 31 }); 32 33 var server =http.createServer( function ( request ,response ){ 34 var h_name = request.headers.host ; 35 var h_path = url.parse( request.url ).pathname ; 36 37 if( h_path === '/xiaohong' ){ 38 response.setHeader( 'Content-Type' , 'application/octet-stream' ); 39 response.setHeader( 'Content-Disposition' , 'attachment; filename=' + encodeURIComponent(file_name) ); 40 41 for( var buffer_index = 0 ; buffer_index<buffer_box.length ; buffer_index++ ) 42 { 43 response.write( buffer_box[buffer_index] ); 44 } 45 response.end(); 46 } 47 else if( h_name === 'localhost:8000' && h_path === '/exit' ){ 48 response.end('Bye!'); 49 server.close() ; 50 console.log( 'Bye!' ); 51 } 52 else{ 53 response.end( 'Hello, iLinkIT' ); 54 } 55 56 } ); 57 server.listen( 8000 ); 58 console.log( 'HTTP服務器啟動中,端口:8000.....' ); 59 60 }//end else,讀取文件沒有發生錯誤 61 });
關鍵代碼解析如下:
第5行,通過process.argv.splice( 2 )獲得了傳入的命令行參數。
之前我們啟動NodeJS服務器的命令為:node h_ilinkit_1.js,而要傳入參數之后,執行的命令為node h_ilinkit_2.js d:\ilinkit_logo.rar,
process.argv會將輸入命令行的所有的內容都獲取到,包括node h_ilinkit_2.js部分,我們通過調用splice( 2 ),獲得傳入的第3個參數的內容,將前面的兩個字符串剔除掉。
第6行,將輸入命令行的內容中,除了node h_ilinkit_2.js之外的內容合並在一起,作為文件路徑。
驗證方式如下:
1. 啟動服務器:打開命令行,進入js腳本所在的位置,執行:node h_ilinkit_2.js d:\ilinkit_logo.rar,如下圖所示:
3. 打開瀏覽器,輸入:http://localhost:8000/xiaohong,顯示如下:
可見,我們已經實現了通過命令行傳入參數的功能,因為我們傳入的是d:\ilinkit_logo.rar,所以,客戶端提交請求后,下載到的文件也是ilinkit_logo.rar。
簡單回顧一下:
1. 借助NodeJS的服務器響應機制,通過給服務器提交/exit的請求,實現服務器的退出操作。
2. 通過在啟動NodeJS時,向服務器傳入共享文件的路徑,實現共享文件的自定義,這樣,想共享哪個文件,就可以共享哪個文件。
關於通過愛蓮(iLinkIT)這個小項目講解NodeJS的特性的文章,到此已經全部結束,不知道您是否已經對NodeJS產生愛戀^_^?更深入的了解NodeJS,推薦您根據業務需要,找專門的專著來閱讀,另外,泡NodeJS社區論壇,也是一個擴大認識,解決問題的好辦法,沒錯,就是這個社區:https://cnodejs.org/
感謝您的捧場,晚安^_^~~
-----------------------愛蓮(iLinkIT)系列文章------------------------------------------