-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.coffee
62 lines (46 loc) · 1.8 KB
/
server.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
55
56
57
58
59
60
61
62
"use strict"
http = require("http")
path = require("path")
express = require("express")
api = require("./lib/api")
routes = require("./lib/routes")
app = express()
# TODO: Using global variables is ugly; will pass in variable in the constructor.
global._app = app
# all environments
app.set "port", process.env.PORT or 3000
app.engine 'html', require('ejs').renderFile
app.set 'view engine', 'html'
app.enable 'trust proxy'
# development only
if "development" is app.get("env")
app.locals { appPath: path.join(__dirname, "app") }
app.use express.static(path.join(__dirname, ".tmp"))
app.use express.static(path.join(__dirname, "app"))
app.use express.errorHandler()
app.set 'views', path.join(__dirname, "app")
# production only
else
app.locals { appPath: path.join(__dirname, "public") }
app.use express.favicon(path.join(__dirname, "public/favicon.ico"))
app.use express.static(path.join(__dirname, "public"))
app.set 'views', path.join(__dirname, "public")
#app.use express.logger(stream: {write: (msg, encode) -> logger.info(msg)})
# Setup data.
app.use express.cookieParser() # needed for session cookie.
#app.use express.bodyParser()
app.use express.urlencoded()
app.use express.json()
app.use express.methodOverride()
#
app.use app.router # api router
# All the APIs begin here.
app.get "/api/awesomeThings", api.awesomeThings
app.get "/api/rank", api.utils.getRank
app.get "/api/languages", api.utils.getLanguages
app.all "/api/*", (req, res) -> res.json 404, error: "API Not Found."
app.get '/views/*', routes.views
app.get '/*', (req, res) ->
res.sendfile path.join(__dirname, "#{if 'development' is app.get('env') then 'app' else 'public'}/index.html")
http.createServer(app).listen app.get("port"), ->
console.log "Express server listening on port #{app.get('port')} in #{app.get('env')} mode."