-
Notifications
You must be signed in to change notification settings - Fork 55
/
server.js
107 lines (83 loc) · 2.25 KB
/
server.js
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
var express = require('express');
var cookieParser = require('cookie-parser');
var compress = require('compression');
var session = require('express-session');
var bodyParser = require('body-parser');
var logger = require('morgan');
var errorHandler = require('errorhandler');
var methodOverride = require('method-override');
var path = require('path');
var expressValidator = require('express-validator');
/**
* Create Express server.
*/
var app = express();
var hour = 3600000;
var day = hour * 24;
var week = day * 7;
/**
* Load controllers.
*/
var API = require('./controllers/api');
/**
* Express configuration.
*/
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(compress());
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(expressValidator());
app.use(methodOverride());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'), { maxAge: week }));
/**
* Application routes.
*/
app.get('/', API.index);
app.get('/api/', API.index);
app.get('/api/v1', API.getAll);
app.get('/api/v1/callingcode/:callingCode', API.callingCode)
app.get('/api/v1/region/:regionName', API.region)
app.get('/api/v1/subregion/:subregionName', API.subregion)
app.get('/api/v1/currency/:currency_code', API.currency)
/**
* 500 Error Handler.
* As of Express 4.0 it must be placed at the end, after all routes.
*/
app.use(function(err, req, res, next){
// treat as 404
if (err.message
&& (~err.message.indexOf('not found')
|| (~err.message.indexOf('Cast to ObjectId failed')))) {
return next()
}
// log it
// send emails if you want
// error page
res.status(500).json({
error: err,
pkg: pkg,
CONFIG: CONFIG
})
})
// assume 404 since no middleware responded
app.use(function(req, res, next){
res.status(404).json({
url: req.originalUrl,
message: "Sorry, that page does not exist",
code: 34
})
})
if (app.get('env') === 'development') {
app.use(errorHandler())
}
/**
* Start Express server.
*/
app.listen(app.get('port'), function() {
console.log("✔ Express server listening on port %d in %s mode", app.get('port'), app.get('env'));
});
module.exports = app;