衆所周知的,JavaScript是運行在瀏覽器的腳本語言,JavaScript一般做爲客戶端程序設計語言使用,以JavaScript寫出的程序常在用戶的瀏覽器上運行。直至Nodejs的出現,Node.js大部分基本模塊都用JavaScript語言編寫,Node.js的出現使JavaScript也能用於服務器端編程。Node.js含有一系列內置模塊,使得程序能夠脫離Apache HTTP Server或IIS,做爲獨立服務器運行。html
Node.js容許經過JavaScript和一系列模塊來編寫服務器端應用和網絡相關的應用。node
核心模塊包括文件系統I/O、網絡(HTTP、TCP、UDP、DNS、TLS/SSL等)、二進制數據流、加密算法、數據流等等。git
運用Node.js的相關框架能夠快速的完成應用的開發,經常使用的框架有Express.js、Koa.js、Socket.IO和Connect等。github
Node.js主要用於編寫像Web服務器同樣的網絡應用,這和PHP和Python是相似的。可是Node.js與其餘語言最大的不一樣之處在於,PHP等語言是阻塞的(只有前一條命令執行完畢纔會執行後面的命令),而Node.js是非阻塞的(多條命令能夠同時被運行,經過回調函數得知命令已結束運行)。算法
Node.js是事件驅動的。開發者能夠在不使用線程的狀況下開發出一個可以承載高併發的服務器。shell
Node.js使用Google V8JavaScript 引擎,由於V8是基於BSD許可證的開源軟件且速度很是快而且專一於網絡功能,在HTTP、DNS、TCP等方面更加成熟。npm
Node.js的包管理器npm可完成相關依賴包的模塊下載。編程
應用說明:經過啓動本地服務器,完成圖片上傳並展現的功能。api
HTTP服務器模塊(HTTP服務器):http://nodejs.cn/api/http.html#http_http瀏覽器
fs模塊(文件系統):http://nodejs.cn/api/fs.html#fs_file_system
url模塊(網址):http://nodejs.cn/api/url.html#url_class_url
formidable模塊(處理文件上傳,ps:該模塊須要npm install安裝):https://cnodejs.org/topic/4f16442ccae1f4aa2700104d
1.啓動服務器,發送HTTP請求
2.經過獲取請求參數,執行路由跳轉或處理程序
1.抽取出主頁面index.js(解耦)
/** * Created by aaron. */ var server=require('./server'); var router=require('./router'); var requestHandlers=require('./requestHandlers') var handle={} handle['/']=requestHandlers.start, handle['/start']=requestHandlers.start, handle['/upload']=requestHandlers.upload, handle['/show']=requestHandlers.show; server.start(router.route,handle);
2.建立服務器並監聽客戶端請求事件server.js
/** * Created by aaron. */ var http=require('http'); var url=require('url'); function start(route,handle) { function onRequest(request,response) { var postData=''; var pathname=url.parse(request.url).pathname; console.log('Request for'+pathname+'received'); //node-formidable會對postData及setEncoding處理 /*request.setEncoding('utf8'); request.addListener('data',function (postDataChunk) { postData+=postDataChunk; console.log('Received POST data chunk'+postDataChunk+'.'); }); request.addListener('end',function () { route(handle,pathname,response,postData); })*/ route(handle,pathname,response,request); } http.createServer(onRequest).listen(8888); console.log('Server has started.'); } exports.start=start;
3.獲取請求參數並執行路由跳轉router.js
/** * Created by aaron. */ function route(handle,pathname,response,request) { console.log('About to route a request for'+pathname); if(typeof handle[pathname]==='function'){ handle[pathname](response,request); }else{ console.log('No request handler found for'+pathname); response.writeHead(404,{'Content-type':'text/plain'}); response.write('404 Not found'); response.end(); } } exports.route=route;
4.請求處理程序並做出響應requestHandler.js
/** * Created by aaron. */ //執行非阻塞操做,從node來執行一個shell命令 var exec=require('child_process').exec; var querystring=require('querystring'); var fs=require('fs'); var formidable=require('formidable'); function start(response) { console.log('Request handler "start" was called.'); var body='<html>' + '<head>' + '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>' + '</head>' + '<body>' + '<form action="/upload" method="post" enctype="multipart/form-data">' + '<input type="file" name="upload"/>' + '<br/>'+ '<input type="submit" value="Upload file"/>'+ '</form>' + '</body>'+ '</html>'; response.writeHead(200,{'Content-type':'text/html'}); response.write(body); response.end(); } function upload(response,request) { console.log('Request handler "upload " was called.'); var form=new formidable.IncomingForm(); form.parse(request,function (error,fields,files) { console.log('parsing done:<br/>'+files.upload.path); fs.renameSync(files.upload.path,'C:/temp/test.jpg'); response.writeHead(200,{'Content-type':'text/html'}); response.write('received image:<br/>'); response.write('<img src="/show"/>'); response.end(); }) /*response.writeHead(200,{'Content-type':'text/plain'}); response.write('You`ve sent the text:'+ querystring.parse(postData).text ); response.end();*/ } function show(response,postData) { console.log('Request handler "show" was called.'); fs.readFile('C:/temp/test.jpg','binary',function (error,file) { if(error){ response.writeHead(500,{'Content-type':'text/plain'}); response.write(error+'\n'); response.end(); }else{ response.writeHead(200,{'Content-type':'image/jpg'}); response.write(file,'binary'); response.end(); } }) } exports.start=start; exports.upload=upload; exports.show=show;
選擇圖片--> 上傳 --> 展現圖片
fs.renameSync()執行函數時若是執行圖片上傳的盤不是系統盤時會提示報錯,解決方案是圖片路徑設置爲絕對路徑並設置爲系統盤。
GitHub源碼:https://github.com/PCAaron/node
《Node.js入門》:https://www.nodebeginner.org/index-zh-cn.html
維基百科:https://zh.wikipedia.org/wiki/Node.js
Node.js中文網:http://nodejs.cn/api/