-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (85 loc) · 2.99 KB
/
index.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var http = require('http')
, path = require('path')
, url = require('url')
, fs = require('fs')
var dir = path.join( process.cwd(), process.argv[2] || '' )
, port = parseInt(process.argv[3]) || 8080
// problem with nproxy
// dont support relative path
// dont support SSL
// must need map file
// map file name and easy format: yml or json?
// TODO
// alternative proxy features?
//
// use .stab.json or stab.yml?
// /abc/ for regex
// *abc for wildchar
// absolute replace ?: baidu.com/file file A , A standfor absolute, R standfor Regexp
// quick cli replace : stab baidu.com/file file or stab baidu.com/ ./ (dir replace)
// rules
// "file1 > ./file2, dir/ > ./"
// problem cache? disable cache
http.createServer(function(req, res) {
var info = url.parse(req.url, true)
var uri = info.pathname
, filepath = path.join(dir, uri)
, delay = info.query.delay
var log = 'Request: ' +filepath
if ( isFinite(delay) ) {
log += ' with Delay ' +delay +'ms'
}
console.log(log)
// res.setHeader('Access-Control-Allow-Origin', '*')
fs.stat(filepath, function(err, stats) {
if (err) {
res.writeHead(404)
res.end('404')
return
}
if (stats.isFile()) {
res.writeHead(200, {
'Content-Type' :
(function () { // mimeType
switch (path.extname(filepath).slice(1)) {
case 'html' :
return 'text/html'
case 'xml' :
return 'text/xml'
case 'css' :
return 'text/css'
case 'js' :
return 'text/javascript'
case 'jpg' :
return 'image/jpg'
case 'gif' :
return 'image/gif'
case 'png' :
return 'image/png'
default :
return 'text/plain'
}
})()
})
if ( isFinite(delay) ) {
setTimeout(function () {
fs.createReadStream(filepath).pipe(res)
}, delay)
} else {
fs.createReadStream(filepath).pipe(res)
}
} else if (stats.isDirectory()) {
fs.readdir(filepath, function (err, files) {
res.writeHead(200, {'Content-Type' : 'text/html'})
res.write('<!DOCTYPE html><html><body><h1>Directory ' +uri +'</h1><ul>')
files.forEach(function (name, i) {
res.write('<li><a href="' +path.join(uri, name) +'">' +name +'</a></li>')
})
res.end('</ul></body></html>')
})
} else {
res.end('WTF!!')
}
})
}).listen(port)
console.log('Server running in "' +dir +'" on port: ' +port)