                 else
                     res.send 401
             )
+### Default 404 middleware ###
+#app.use routes.error('Page not found :(', 404)
 
+#Application name.
+app.locals.title = "Home Again API"
+
+
+# Init mongo connection only once
+require('./models/connection').initiate (db) ->
+    console.log('Connection is now established with mongoDB on homeAgain.')
+
+
+###
+    Configuration of the application.
+###
 app.configure 'production', ->
     app.use express.limit '5mb'
 

express = require 'express'
{join} = require 'path'
{config} = require './config'
routes = require './routes'
backbone = require './routes/index'
collector = require './routes/collector'

    app.locals.pretty = true


###
    Actions
###
app.all(/^\/admin.*$/, backbone.index)
app.post '/collector/collect', loginMiddleware, collector.collect
###Sensor actions###
app.get '/sensor/:sensor_id', loginMiddleware, sensor.get
app.get '/sensor', loginMiddleware, sensor.all
app.put('/sensor/:sensor_id', loginMiddleware, sensor.put)

###Room actions###
app.get '/room/:room_id', loginMiddleware, room.get
app.post '/room/', loginMiddleware, room.post
app.get '/room', loginMiddleware, room.all

app.post '/login', login.post
app.get '/test', routes.test('Mocha Tests')

# Server launching
server.listen app.get('port'), ()->
    console.log('Express server listening on port', app.get('port'))

module.exports = exports = app
