停止nodejs的方法:1、通過“Ctrl+C”來關閉NodeJS服務器;2、判斷客戶端提交的請求信息,并調用“server.close()”關閉服務器即可。
本文操作環(huán)境:windows7系統(tǒng)、nodejs10.16.2版、Dell G3電腦。
怎么停止nodejs?
NodeJS服務器退出:完成任務,優(yōu)雅退出 :
首先,不能共享完畢之后,都通過Ctrl+C來關閉NodeJS服務器。
其次,如果僅僅能向客戶端提供d:ilinkit_logo.png文件的下載,是沒有意義的,共享哪個文件,應該可以通過傳入的參數來指定。
老規(guī)矩,先上一個圖:
我們首先來實現退出功能,如果客戶端向服務器提交了http://localhost:8000/exit的請求,我們就執(zhí)行服務器的退出操作。
上一篇文章我們已經能夠識別出/xiaohong的請求,所以這個實現起來很簡單,代碼如下:
var http = require( 'http' ); var fs = require('fs'); var url = require( 'url' ); var file_path = "D:\ilinkit_logo.png" ; var file_stream ; var buffer_box = [] ; var file_length = 0 ; var file_name = file_path.substr( file_path.lastIndexOf('\')+1 ); fs.stat( file_path , function ( err , stat ){ if (err) { if ('ENOENT' == err.code) { console.log( 'File does not exist...' ); } else { console.log( 'Read file exception...' ); } } else { file_stream = fs.createReadStream( file_path ); file_stream.on( 'data' , function( chunk ){ buffer_box.push( chunk ) ; file_length += chunk.length ; } ); file_stream.on( 'end' , function( ){ console.log( "文件讀取完畢" ); } ); file_stream.on('error', function(err){ console.log( "文件讀取失敗!" ); }); var server =http.createServer( function ( request ,response ){ var h_name = request.headers.host ; var h_path = url.parse( request.url ).pathname ; if( h_path === '/xiaohong' ){ response.setHeader( 'Content-Type' , 'application/octet-stream' ); response.setHeader( 'Content-Disposition' , 'attachment; filename=' + encodeURIComponent(file_name) ); for( var buffer_index = 0 ; buffer_index<buffer_box.length ; buffer_index++ ) { response.write( buffer_box[buffer_index] ); } response.end(); } else if( h_name === 'localhost:8000' && h_path === '/exit' ){ response.end('Bye!'); server.close() ; console.log( 'Bye!' ); } else{ response.end( 'Hello, iLinkIT' ); } } ); server.listen( 8000 ); console.log( 'HTTP服務器啟動中,端口:8000.....' ); }//end else,讀取文件沒有發(fā)生錯誤 });
關鍵的代碼解析如下:
第33行,我們通過request對象獲取客戶端請求的主機及端口內容。
第46行~第50行,我們判斷客戶端提交的請求信息,如果是http://localhost:8000/exit,則調用server.close()關閉服務器。為什么要判斷是不是localhost提交的請求?因為我們希望僅僅在服務器本地提交的請求才能關閉NodeJS服務器。
驗證方式如下:
1. 啟動服務器:打開命令行,進入js腳本所在的位置,執(zhí)行:node h_ilinkit_1.js。
2. 打開瀏覽器,輸入:http://localhost:8000,顯示如下:
說明當前服務器啟動正常。
3. 打開瀏覽器,輸入:http://localhost:8000/exit。
提示NodeJS服務器已經關閉,我們把瀏覽器關閉之后,發(fā)現服務器已經正常退出,如下所示。
這樣,我們就沒必要每次為了退出服務器,都去按Ctrl + C了。
通過請求退出服務器就介紹到這里,接下來我們再看一下,如果在啟動NodeJS服務器的時候,給它傳入參數。對應到我們愛蓮(iLinkIT)的場景,希望能夠將要共享的文件的路徑作為參數傳遞給NodeJS服務器,服務器根據傳入的文件路徑讀取數據到緩沖區(qū),接受客戶端的響應。
代碼如下:
var http = require( 'http' ); var fs = require('fs'); var url = require( 'url' ); var args = process.argv.splice( 2 ); var file_path = args.join( '' ) ; var file_stream ; var buffer_box = [] ; var file_length = 0 ; var file_name = file_path.substr( file_path.lastIndexOf('\')+1 ); fs.stat( file_path , function ( err , stat ){ if (err) { if ('ENOENT' == err.code) { console.log( 'File does not exist...' ); } else { console.log( 'Read file exception...' ); } } else { file_stream = fs.createReadStream( file_path ); file_stream.on( 'data' , function( chunk ){ buffer_box.push( chunk ) ; file_length += chunk.length ; } ); file_stream.on( 'end' , function( ){ console.log( "文件讀取完畢" ); } ); file_stream.on('error', function(err){ console.log( "文件讀取失?。?quot; ); }); var server =http.createServer( function ( request ,response ){ var h_name = request.headers.host ; var h_path = url.parse( request.url ).pathname ; if( h_path === '/xiaohong' ){ response.setHeader( 'Content-Type' , 'application/octet-stream' ); response.setHeader( 'Content-Disposition' , 'attachment; filename=' + encodeURIComponent(file_name) ); for( var buffer_index = 0 ; buffer_index<buffer_box.length ; buffer_index++ ) { response.write( buffer_box[buffer_index] ); } response.end(); } else if( h_name === 'localhost:8000' && h_path === '/exit' ){ response.end('Bye!'); server.close() ; console.log( 'Bye!' ); } else{ response.end( 'Hello, iLinkIT' ); } } ); server.listen( 8000 ); console.log( 'HTTP服務器啟動中,端口:8000.....' ); }//end else,讀取文件沒有發(fā)生錯誤 });
關鍵代碼解析如下:
第5行,通過process.argv.splice( 2 )獲得了傳入的命令行參數。
之前我們啟動NodeJS服務器的命令為:node h_ilinkit_1.js,而要傳入參數之后,執(zhí)行的命令為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腳本所在的位置,執(zhí)行: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時,向服務器傳入共享文件的路徑,實現共享文件的自定義,這樣,想共享哪個文件,就可以共享哪個文件。
推薦學習:《node.js視頻教程》