建站服務(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é)議模塊
var
http = require(
\'http\'
);
//url解析模塊
var
url = require(
\'url\'
);
//文件系統(tǒng)模塊
var
fs = require(
fs
);
//路徑解析模塊
var
path = require(
path
);
1
2
3
4
5
6
7
8
//創(chuàng)建一個(gè)服務(wù)
var
httpServer = 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){
var
hasExt =
true
;
var
requestUrl = request.url;
var
pathName = url.parse(requestUrl).pathname;
//對請求的路徑進(jìn)行解碼,防止中文亂碼
pathName = decodeURI(pathName);
//如果路徑中沒有擴(kuò)展名
if
(path.extname(pathName) ===
\'\'
){
//如果不是以/結(jié)尾的,加/并作301重定向
if
(pathName.charAt(pathName.length-1) !=
/
){
pathName +=
/
;
var
redirect =
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)添加的
}
//獲取資源文件的相對路徑
var
filePath = path.
join
(
http/webroot
,pathName);
//獲取對應(yīng)文件的文檔類型
var
contentType =
this
.getContentType(filePath);
//如果文件名存在
fs.exists(filePath,function(exists){
if
(exists){
response.writeHead(200, {
content-type
:contentType});
var
stream = 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)添加的且不存在,則表示用戶希望訪問的是該目錄下的文件列表
var
html =
<head><meta charset=\'utf-8\'></head>
;
try
{
//用戶訪問目錄
var
filedir = filePath.substring(0,filePath.lastIndexOf(
\'\\\\\'
));
//獲取用戶訪問路徑下的文件列表
var
files = fs.readdirSync(filedir);
//將訪問路徑下的所以文件一一列舉出來,并添加超鏈接,以便用戶進(jìn)一步訪問
for
(
var
i
in
files){
var
filename = 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)