-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (30 loc) · 912 Bytes
/
server.js
File metadata and controls
34 lines (30 loc) · 912 Bytes
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
/**
* Created by zhongyufei on 2016/5/11.
*/
var http = require('http');
var path = require('path');
var fs = require('fs');
http.createServer(function(req,res){
if(req.url == '/favicon.ico'){
res.end('404')
}else if(req.url == '/'){
var indexHtml = path.join(__dirname,'test','index.html');
console.log(indexHtml);
fs.createReadStream(indexHtml).pipe(res);
}else{
var pathname = path.join(__dirname,req.url);
fs.stat(pathname,function(err,stats){
if(!stats) {
res.end("404");
return;
}
if(stats.isDirectory()){
res.end("It's a Directory")
}else if(stats.isFile()){
fs.createReadStream(pathname).pipe(res);
}
});
}
}
).listen(3000);
console.log('server start at port:3000...');