内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

使用Nodejs搭建Web服務(wù)器

建站服務(wù)器

1

延慶網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),延慶網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為延慶1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個(gè)售后服務(wù)好的延慶做網(wǎng)站的公司定做!

2

3

4

5

6

7

8

//http協(xié)議模塊

varhttp = require(\'http\');

//url解析模塊

varurl = require(\'url\');

//文件系統(tǒng)模塊

varfs = require(fs);

//路徑解析模塊

varpath = require(path);

1

2

3

4

5

6

7

8

//創(chuàng)建一個(gè)服務(wù)

varhttpServer = http.createServer(this.proce***equest.bind(this));

//在指定的端口監(jiān)聽服務(wù)

httpServer.listen(port,function(){

console.log([HttpServer][Start],runing at http://+ip+:+port+/);

console.timeEnd([HttpServer][Start]);

});

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

proce***equest:function(request,response){

varhasExt =true;

varrequestUrl = request.url;

varpathName = url.parse(requestUrl).pathname;

//對請求的路徑進(jìn)行解碼,防止中文亂碼

pathName = decodeURI(pathName);

//如果路徑中沒有擴(kuò)展名

if(path.extname(pathName) ===\'\'){

//如果不是以/結(jié)尾的,加/并作301重定向

if(pathName.charAt(pathName.length-1) !=/){

pathName +=/;

varredirect =http://+request.headers.host + pathName;

response.writeHead(301, {

location:redirect

});

response.end();

}

//添加默認(rèn)的訪問頁面,但這個(gè)頁面不一定存在,后面會(huì)處理

pathName +=index.html;

hasExt =false;//標(biāo)記默認(rèn)頁面是程序自動(dòng)添加的

}

//獲取資源文件的相對路徑

varfilePath = path.join(http/webroot,pathName);

//獲取對應(yīng)文件的文檔類型

varcontentType =this.getContentType(filePath);

//如果文件名存在

fs.exists(filePath,function(exists){

if(exists){

response.writeHead(200, {content-type:contentType});

varstream = fs.createReadStream(filePath,{flags:r,encoding:null});

stream.on(error, function() {

response.writeHead(500,{content-type:text/html});

response.end(<h2>500 Server Error</h2>);

});

//返回文件內(nèi)容

stream.pipe(response);

}else{//文件名不存在的情況

if(hasExt){

//如果這個(gè)文件不是程序自動(dòng)添加的,直接返回404

response.writeHead(404, {content-type:text/html});

response.end(<h2>404 Not Found</h2>);

}else{

//如果文件是程序自動(dòng)添加的且不存在,則表示用戶希望訪問的是該目錄下的文件列表

varhtml =<head><meta charset=\'utf-8\'></head>;

try{

//用戶訪問目錄

varfiledir = filePath.substring(0,filePath.lastIndexOf(\'\\\\\'));

//獲取用戶訪問路徑下的文件列表

varfiles = fs.readdirSync(filedir);

//將訪問路徑下的所以文件一一列舉出來,并添加超鏈接,以便用戶進(jìn)一步訪問

for(variinfiles){

varfilename = files[i];

html +=<div><a href=\'+filename+\'>+filename+</a></div>;

}

}catch(e){

html +=<h2>您訪問的目錄不存在</h2>

}

response.writeHead(200, {content-type:text/html});

response.end(html);

}

}

});

}

新聞標(biāo)題:使用Nodejs搭建Web服務(wù)器
文章位置:http://www.rwnh.cn/article2/cpeoic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、微信小程序標(biāo)簽優(yōu)化、搜索引擎優(yōu)化、移動(dòng)網(wǎng)站建設(shè)網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
陆河县| 柏乡县| 宁夏| 伊通| 武清区| 塔河县| 九龙县| 嘉义县| 崇义县| 莎车县| 双鸭山市| 循化| 台山市| 石门县| 电白县| 通渭县| 缙云县| 丹凤县| 乌兰察布市| 特克斯县| 广安市| 新乡县| 邵东县| 泗水县| 信宜市| 郯城县| 澄江县| 句容市| 新野县| 凤凰县| 广平县| 临安市| 沁源县| 精河县| 龙山县| 吴堡县| 岳阳市| 神农架林区| 克什克腾旗| 新建县| 旺苍县|