11// Setup
2+
3+ const uriFormat = require ( 'mongodb-uri' )
4+ function encodeMongoURI ( urlString ) {
5+ if ( urlString ) {
6+ let parsed = uriFormat . parse ( urlString )
7+ urlString = uriFormat . format ( parsed ) ;
8+ }
9+ return urlString ;
10+ } ;
11+
12+
213var application_root = __dirname ,
314 secret = process . env . SECRET ,
415 port = process . env . PORT ,
5- db = process . env . DB ,
16+ db = encodeMongoURI ( process . env . DB ) ,
617 consumer = process . env . CONSUMER ,
718 version = process . env . VERSION ,
819 path = require ( "path" ) ,
@@ -30,6 +41,7 @@ var allowCrossDomain = function(req, res, next) {
3041 }
3142} ;
3243
44+
3345// Schemas
3446var Schema = mongoose . Schema ;
3547
@@ -171,8 +183,6 @@ var Annotation = new Schema({
171183 }
172184} ) ;
173185
174- var AnnotationModel = mongoose . model ( 'Annotation' , Annotation ) ;
175-
176186// DB
177187mongoose . connect ( db ) ;
178188
@@ -183,13 +193,27 @@ app.use(express.urlencoded({
183193} ) ) ;
184194app . use ( express . json ( ) ) ;
185195app . use ( methodOverride ( ) ) ;
186-
196+ app . use ( lessMiddleware ( __dirname + '/public' , {
197+ render :{
198+ compress : true
199+ }
200+ } ) ) ;
201+
202+ app . use ( express . static ( path . join ( application_root , "public" ) ) ) ;
203+ app . use ( errorhandler ( {
204+ dumpExceptions : true ,
205+ showStack : true
206+ } ) ) ;
207+
187208
188209Annotation . pre ( 'save' , function ( next ) {
189210 this . id = this . _id ;
190211 next ( ) ;
191212} ) ;
192213
214+ var AnnotationModel = mongoose . model ( 'Annotation' , Annotation ) ;
215+
216+
193217// ROUTES
194218app . get ( '/api' , function ( req , res ) {
195219 res . send ( 'Annotations API is running' ) ;
@@ -440,18 +464,6 @@ app.delete('/api/annotations/:id', tokenOK, function(req, res) {
440464 } ) ;
441465} ) ;
442466
443- // Middleware config
444- app . use ( lessMiddleware ( __dirname + '/public' , {
445- render :{
446- compress : true
447- }
448- } ) ) ;
449-
450- app . use ( express . static ( path . join ( application_root , "public" ) ) ) ;
451- app . use ( errorhandler ( {
452- dumpExceptions : true ,
453- showStack : true
454- } ) ) ;
455467
456468// Authentication
457469function tokenOK ( req , res , next ) {
0 commit comments