Cross Reference: delay.js
xref
: /
yui3
/
src
/
get
/
tests
/
getfiles
/
delay.js
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
99030181384118f852c69f20f3b931bf796ec8fa
Matt Sweeney
var
http
=
require
(
'http'
);
99030181384118f852c69f20f3b931bf796ec8fa
Matt Sweeney
var
url
=
require
(
'url'
);
99030181384118f852c69f20f3b931bf796ec8fa
Matt Sweeney
var
fs
=
require
(
'fs'
);
99030181384118f852c69f20f3b931bf796ec8fa
Matt Sweeney
var
path
=
require
(
'path'
);
99030181384118f852c69f20f3b931bf796ec8fa
Matt Sweeney
99030181384118f852c69f20f3b931bf796ec8fa
Matt Sweeney
var
EXT_TO_TYPE
= {
"js"
:
"
text
/
javascript
"
,
"css"
:
"
text
/
css
"
}
function
rand
(
min
,
max
) {
return
Math
.
floor
(
Math
.
random
() * (
max
-
min
) +
min
);
}
function
send
(
res
,
code
,
content
,
type
) {
res
.
writeHead
(
code
, {
'Content-Type'
:
type
});
res
.
end
(
content
);
}
function
sendResponse
(
res
,
filepath
,
delay
) {
var
DEFAULT_JS_CONTENT
=
"console.log('Server Delay:"
+
delay
+
"');"
;
if
(
filepath
===
'/'
) {
send
(
res
,
200
,
DEFAULT_JS_CONTENT
,
EXT_TO_TYPE
[
"js"
]);
}
else
{
var
extension
=
path
.
extname
(
filepath
).
substring
(
1
);
var
contentType
=
EXT_TO_TYPE
[
extension
] ||
"
text
/
html
"
;
// Convert to "server" root
var
filepath
=
__dirname
+
filepath
;
path
.
exists
(
filepath
,
function
(
exists
) {
if
(
exists
) {
fs
.
readFile
(
filepath
,
function
(e,
content
) {
if
(!e) {
content
=
"/* Delayed: "
+
delay
+
"*/\n"
+
content
;
send
(
res
,
200
,
content
,
contentType
);
}
else
{
send
(
res
,
500
,
"Error"
,
"
text
/
html
"
);
}
});
}
else
{
send
(
res
,
404
,
"File Not Found"
,
"
text
/
html
"
);
}
});
}
}
http
.
createServer
(
function
(
req
,
res
) {
var
parts
=
url
.
parse
(
req
.
url
,
true
);
console
.
log
(
"Request:"
+
parts
.
pathname
);
if
(
parts
.
pathname
.
indexOf
(
"favicon.ico"
) !== -
1
) {
res
.
writeHead
(
404
);
res
.
end
(
""
);
}
else
{
var
delay
=
parts
.
query
.
delay
||
rand
(
100
,
2000
);
console
.
log
(
"delay:"
+
delay
);
setTimeout
(
function
() {
sendResponse
(
res
,
parts
.
pathname
,
delay
);
},
delay
);
}
}).
listen
(
8014
);
console
.
log
(
'Server running on port 8014'
);