FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JsObjects/JavaScript/Games/Server.js at master · charliecalvert/JsObjects · GitHub
charliecalvert
/
JsObjects
Public
Notifications
You must be signed in to change notification settings
Fork
53
Star
22
Code
Issues
1
Pull requests
1
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JsObjects
/
JavaScript
/
Games
/
Server.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
108 lines (101 loc) · 3.34 KB
Breadcrumbs
JsObjects
/
JavaScript
/
Games
/
Server.js
Copy path
File metadata and controls
108 lines (101 loc) · 3.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
98
99
100
101
102
103
104
105
106
107
108
var
http
=
require
(
'http'
)
;
var
url
=
require
(
'url'
)
;
var
port
=
process
.
env
.
PORT
||
30025
;
var
fs
=
require
(
'fs'
)
;
var
path
=
require
(
'path'
)
;
function
getPath
(
request
)
{
'use strict'
;
return
url
.
parse
(
request
.
url
)
.
pathname
;
}
// Thanks to Wallace: http://stackoverflow.com/a/1203361/253576
function
getExtension
(
fileName
)
{
'use strict'
;
var
fileNameArray
=
fileName
.
split
(
"."
)
;
if
(
fileNameArray
.
length
===
1
||
(
fileNameArray
[
0
]
===
""
&&
fileNameArray
.
length
===
2
)
)
{
return
""
;
}
return
fileNameArray
.
pop
(
)
.
toLowerCase
(
)
;
}
// Add endsWith method to String
if
(
typeof
String
.
prototype
.
endsWith
!==
'function'
)
{
String
.
prototype
.
endsWith
=
function
(
suffix
)
{
'use strict'
;
return
this
.
indexOf
(
suffix
,
this
.
length
-
suffix
.
length
)
!==
-
1
;
}
;
}
var
findFile
=
function
(
dir
,
fileName
,
done
)
{
'use strict'
;
var
results
=
[
]
;
var
ext
=
path
.
extname
(
fileName
)
;
fs
.
readdir
(
dir
,
function
(
err
,
list
)
{
if
(
err
)
return
done
(
err
)
;
var
pending
=
list
.
length
;
if
(
!
pending
)
return
done
(
null
,
results
)
;
list
.
forEach
(
function
(
file
)
{
file
=
dir
+
'/'
+
file
;
fs
.
stat
(
file
,
function
(
err
,
stat
)
{
if
(
stat
&&
stat
.
isDirectory
(
)
)
{
findFile
(
file
,
fileName
,
function
(
err
,
res
)
{
results
=
results
.
concat
(
res
)
;
if
(
!
--
pending
)
done
(
null
,
results
)
;
}
)
;
}
else
{
if
(
path
.
extname
(
file
)
===
ext
)
{
console
.
log
(
path
.
basename
(
file
)
+
" -- "
+
fileName
)
;
if
(
file
.
endsWith
(
fileName
)
)
{
file
=
file
.
replace
(
/
\\
/
g
,
'\/'
)
;
results
.
push
(
file
)
;
}
}
if
(
!
--
pending
)
done
(
null
,
results
)
;
}
}
)
;
}
)
;
}
)
;
}
;
function
loadContent
(
request
,
response
)
{
'use strict'
;
var
path
=
getPath
(
request
)
;
console
.
log
(
"Request for "
+
path
+
" received."
)
;
if
(
getExtension
(
path
)
===
'css'
)
{
findFile
(
__dirname
,
path
.
replace
(
'\/'
,
''
)
,
function
(
err
,
results
)
{
console
.
log
(
"New Path: "
+
results
[
0
]
)
;
var
css
=
fs
.
readFileSync
(
results
[
0
]
)
;
response
.
writeHead
(
200
,
{
'Content-Type'
:
'text/css'
}
)
;
response
.
write
(
css
)
;
response
.
end
(
)
;
}
)
;
}
else
if
(
getExtension
(
path
)
===
'html'
)
{
var
html
=
fs
.
readFileSync
(
__dirname
+
path
)
;
response
.
writeHead
(
200
,
{
'Content-Type'
:
'text/html'
}
)
;
response
.
write
(
html
)
;
response
.
end
(
)
;
}
else
if
(
getExtension
(
path
)
===
'js'
)
{
findFile
(
__dirname
,
path
.
replace
(
'\/'
,
''
)
,
function
(
err
,
results
)
{
var
javaScript
=
fs
.
readFileSync
(
results
[
0
]
)
;
response
.
writeHead
(
200
,
{
'Content-Type'
:
'text/javascript'
}
)
;
response
.
write
(
javaScript
)
;
response
.
end
(
)
;
}
)
;
}
else
if
(
getExtension
(
path
)
===
'png'
||
getExtension
(
path
)
===
'gif'
||
getExtension
(
path
)
===
'jpg'
)
{
findFile
(
__dirname
,
path
.
replace
(
'\/'
,
''
)
,
function
(
err
,
results
)
{
fs
.
readFile
(
results
[
0
]
,
"binary"
,
function
(
err
,
file
)
{
console
.
log
(
"PNG detected"
)
;
if
(
err
)
{
console
.
log
(
"Error reading binary file"
)
;
response
.
writeHeader
(
500
,
{
"Content-Type"
:
"text/plain"
}
)
;
response
.
write
(
err
+
"\n"
)
;
response
.
end
(
)
;
}
else
{
console
.
log
(
"PNG loaded"
)
;
response
.
writeHeader
(
200
,
{
"Content-Type"
:
"image/png"
}
)
;
response
.
write
(
file
,
"binary"
)
;
response
.
end
(
)
;
}
}
)
;
}
)
;
}
else
{
var
indexHtml
=
fs
.
readFileSync
(
__dirname
+
'/index.html'
)
;
response
.
writeHead
(
200
,
{
'Content-Type'
:
'text/html'
}
)
;
response
.
write
(
indexHtml
)
;
response
.
end
(
)
;
}
}
http
.
createServer
(
loadContent
)
.
listen
(
port
)
;
console
.
log
(
"Server has started:"
+
port
)
;
Back
|
FazBrowse Home
|
New Git URL