nodejs搭建本地服務器
發(fā)布日期:2019-01-05 13:59 | 文章來源:網(wǎng)絡
香港服務器
香港CN2服務器、企業(yè)級CN2 GIA雙程專線高速回國T3機房香港、美國、韓國、海外物理服務器熱銷中!
varhttp=require;varfs=require;varurl=require;//創(chuàng)建服務器http.createServer{//解析請求,包括文件名varpathname=url.parse.pathname;//默認加載路徑if{pathname="/index.html";}//輸出請求的文件名console.log;//獲取后綴,判斷是js還是css文件,如果目錄結構不同,此處需要修改varfirstDir=pathname&&pathname.split1;varContentType={'Content-Type':'text/html'};//js-application/x-javascriptif>-1){ContentType={'Content-Type':'text/css'};}if>-1){ContentType={'Content-Type':'image/png'};}if>-1){ContentType={'Content-Type':'application/x-javascript'}}//從文件系統(tǒng)中去請求的文件內容fs.readFile,function{if{console.log;//HTTP狀態(tài)碼404:NOTFOUND//ContentType:text/plainresponse.writeHead;//發(fā)送響應數(shù)據(jù)response.end;}elseif>-1){//HTTP狀態(tài)碼:200:OK//ContentType:text/plainresponse.writeHead;varimageFilePath=pathname.substr;varstream=fs.createReadStream;varresponseData=;//存儲文件流if{//判斷狀態(tài)stream.on{responseData.push;});stream.on{varfinalData=Buffer.concat;response.write;response.end;});}}else{//HTTP狀態(tài)碼200:OK//ContentType:text/plainresponse.writeHead;//寫會回相應內容response.write);//發(fā)送響應數(shù)據(jù)response.end;}});}).listen;console.log;
Tags:香港服務器,nodejs搭建本地服務器
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
相關文章
下一篇: