-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
94 lines (82 loc) · 2.71 KB
/
index.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
var curl = require('curlrequest');
var express = require('express')
, pg=require('pg')
, morgan = require('morgan')
, bodyParser = require('body-parser')
, methodOverride = require('method-override')
, app = express()
, port = process.env.PORT || 3000
, router = express.Router();
app.use(express.static(__dirname + '/dist/app'));
app.use(bodyParser.urlencoded({
extended: true
}));
router.get('/', function(req, res, next) {
res.render('/app/index.html');
});
// The /db routes require an independently configured database with a table named
// "country" and fields named "name", "id", "iso2", "iso3", and "iso_numeric".
// The file, app/assets/media/countries.sql, can be used to create and populate the table.
router.get('/db', function(req, res, next) {});
router.get('/db/:id', function(req, res) {
if(req.hostname=="bob-angular.herokuapp.com"){
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
try{
var _id=req.param("id");
client.query('SELECT ' + _id + ' FROM country', function(err, result) {
done();
if (err){
res.send("Error " + err);
}else{
res.send(result.rows);
}
});
}catch(oops){
res.set({'Content-Type': 'text/xml'});
res.send("<xml version='1.0'><nocontent reason='trycatch' /></xml>");
}
});
}else{
res.set({'Content-Type': 'text/xml'});
res.send("<xml version='1.0'><nocontent reason='badHost'/></xml>");
}
});
router.get('/db/:id/:val', function(req, res) {
if(req.hostname=="bob-angular.herokuapp.com"){
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
try{
var _id=req.param("id"), val=req.param("val");
var _val= (_id=="id") ? val : "'" + ((_id.indexOf("iso")==-1) ? val : val.toUpperCase()) + "'";
var _str= ' where ' + _id + '=' + _val;
client.query('SELECT * FROM country' + _str, function(err, result) {
done();
if (err){
res.send("Error " + err);
}else{
res.send(result.rows);
}
});
}catch(oops){
res.set({'Content-Type': 'text/xml'});
res.send("<xml version='1.0'><nocontent reason='trycatch' /></xml>");
}
});
}else{
res.set({'Content-Type': 'text/xml'});
res.send("<xml version='1.0'><nocontent reason='badHost'/></xml>");
}
});
router.get('/feed/:feedURL', function(req, res) {
if(req.host=="bob-angular.herokuapp.com"){
curl.request(decodeURIComponent(req.param("feedURL")), function (err, stdout, meta) {
res.set({'Content-Type': 'text/xml'});
res.send(stdout);
});
}else{
res.set({'Content-Type': 'text/xml'});
res.send("<xml version='1.0'><nocontent /></xml>");
}
});
app.use('/', router);
app.listen(port);
console.log('App running on port', port);