-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpostgis_express.js
64 lines (57 loc) · 1.6 KB
/
postgis_express.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
var express = require('express');
var pg = require('pg.js');
global.settings = require('./Settings');
var app = express();
app.use(express.static(__dirname = 'public'));
app.get('/postgis_viewer', function(req, res){
var db = new pg.Client(settings.conString);
var content_type = "text/plain";
var sql = "";
if (!req.query.sql){
res.writeHead(500, {'Content-Type': 'text/plain'});
res.write('No query was given');
res.end();
}
else {
sql = req.query.sql;
}
if (req.query.content_type){
content_type = req.query.content_type.toString();
}
db.connect(function(err) {
if(err) {
console.error('could not connect to postgres', err);
res.writeHead(500, {'Content-Type': 'text/plain'});
res.write(err.toString());
res.end();
db.end();
}
db.query(sql.toString(), function(err, result){
if(err) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.write(err.toString());
res.end();
db.end();
}
else if( result.rowCount > 0 ){
res.writeHead(200, {'Content-Type': content_type});
for(var columnName in result.rows[0]) {
if (content_type.toString().indexOf('image') > -1 ){
res.write("data:" + content_type + ";base64," + result.rows[0][columnName].toString('base64'));
}
else {
res.write(result.rows[0][columnName] + '');
}
}
}
else {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.write('no results');
}
res.end();
db.end();
});
});
});
app.listen(process.env.PORT || 3030);
console.log('listening on 3030');