-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
54 lines (44 loc) · 1.13 KB
/
app.coffee
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
util = require 'util'
fs = require 'fs'
http = require 'http'
visits = 0
responder = (req, res) ->
# Tracking visits
visits += 1 if req.url == '/'
if req.url == '/visits'
res.writeHead 200, "Content-type" : "text/plain"
res.end "Visits: #{visits}"
# Doing some routing
url =
if req.url == '/'
'/index.html'
else if req.url == '/cv'
'/cv.html'
else if req.url == '/posts/latest'
'/posts/hello.md'
else req.url
# Guessing Content-type
head = {}
head['Content-Type'] = (
if /.*\.html/.test url
'text/html'
else if /.*\.css/.test url
'text/css'
else if /.*\.js/.test url
'text/javascript'
else
'text/plain'
)+ '; charset=utf-8'
# Trying to read body
body = ''
try
body = fs.readFileSync('public' + url)
res.writeHead 200, head
catch error
res.writeHead 404, 'Content-Type' : 'text/plain'
body = 'Oops... 404'
res.end body
################# Launching the server ####################
server = http.createServer(responder)
.listen port = process.env.PORT || 3000
util.puts "running on port #{port}"