forked from tinata/tinata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
171 lines (152 loc) · 5.06 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* TINATAPI - An API that provides information useful to travellers. For #FOCHACK
* @author [email protected]
*/
var express = require('express');
var partials = require('express-partials');
var ejs = require('ejs');
var mongoJs = require('mongojs');
var Q = require('q');
var dateFormat = require('dateformat');
var fs = require('fs');
var tinataCountries = require(__dirname + '/lib/tinata-countries');
GLOBAL.db = mongoJs.connect("127.0.0.1/tinatapi", ["countries"]);
// Initialise and configure Express and Express Partials
var app = express();
// Allows all JSON files (e.g. map GeoJSON) to be accessed from any domain
app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
next();
});
app.use(express.static(__dirname + '/public'))
app.use(partials());
app.set('title', 'Tinata');
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.engine('ejs', ejs.__express);
partials.register('.ejs', ejs);
/**
* The homepage
*/
app.get('/', function(req, res, next) {
res.render('index');
});
/**
* The About page
*/
app.get('/about', function(req, res, next) {
res.render('about');
});
/**
* The Maps page
*/
app.get('/maps', function(req, res, next) {
res.render('maps');
});
/**
* Load individual maps
*/
app.get('/maps/:map', function(req, res, next) {
var template = 'maps/'+req.params.map.replace( /[^A-z0-9_-]/ , '');
fs.exists('./views/'+template+'.ejs', function(exists) {
if (exists) {
res.render(template);
} else {
res.status(404).render('page-not-found', { title: "Page not found" });
}
});
});
/**
* List all countries on an HTML page
*/
app.get('/countries(.html)?', function(req, res, next) {
tinataCountries.getAllCountries()
.then(function(countries) {
res.render('countries', { countries: countries } );
});
});
/**
* Return data for all countries in a JSON object
*/
app.get('/countries.json?', function(req, res, next) {
tinataCountries.getAllCountries()
.then(function(countries) {
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.send( JSON.stringify(countries) );
});
});
/**
* List data for a specific country by 2 character ISO code
*/
app.get('/countries/:name', function(req, res, next) {
var path = req.params.name.split('.');
var countryIdentifier = path[0].replace(/_/g, ' ');
// Default response is html. Returns JSON if .json file extention specified
var responseFormat = 'html';
if (path.length > 1)
if (path[1] == 'json')
responseFormat = 'json';
tinataCountries.getCountry(countryIdentifier)
.then(function(country) {
if (country == false) {
// If country not found, return 404
res.status(404).render('page-not-found', { title: "Page not found" });
} if (responseFormat == 'html') {
// Return country information as an HTML page.
// When returning HTML page identify known issues (missing data)
// @todo Refactor, move knownIssues to function in class
var knownIssues = [];
res.render('country', { country: country, knownIssues: knownIssues, dateFormat: dateFormat } );
} else if (responseFormat == 'json') {
// Default (JSON)
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.send( JSON.stringify(country) );
} else {
// If file extention not supported, return 404
res.status(404).render('page-not-found', { title: "Page not found" });
}
});
});
/**
* Return flag page for country (can be looked up by ISO code, name or an alias)
*/
app.get('/countries/:name/flag', function(req, res, next) {
tinataCountries.getCountry(req.params.name)
.then(function(country) {
if (country == false) {
// If country not found, return 404
res.status(404).render('page-not-found', { title: "Page not found" });
} else {
res.render('flag', { country: country } );
}
});
});
/**
* Return flag for country (can be looked up by ISO code, name or an alias)
*/
app.get('/countries/:name/flag.svg', function(req, res, next) {
tinataCountries.getCountry(req.params.name)
.then(function(country) {
if (country == false) {
// If country not found, return 404
res.status(404).render('page-not-found', { title: "Page not found" });
} else {
res.redirect('/img/flags/'+country.iso2.toLowerCase()+'.svg');
}
});
});
/**
* Return information about the status of the database (which data is avalible for what countries)
*/
app.get('/status', function(req, res, next) {
db.countries.find({ '$query': {}, '$orderby': { name: 1 } }, function(err, countries) {
res.render('status', { countries: countries } );
});
});
/**
* Handle all other requests as 404 / Page Not Found errors
*/
app.use(function(req, res, next) {
res.status(404).render('page-not-found', { title: "Page not found" });
});
app.listen(3001);