-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
143 lines (120 loc) · 4.24 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
var express = require('express');
var app = express();
var pg = require('pg');
var bodyParser = require('body-parser');
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/client')); //public
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// views is directory for all template files
app.set('views', __dirname + '/client/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
response.render('pages/index')
});
app.get('/report', function(request, response){
response.render('pages/guidelines')
})
app.get('/map', function(request, response){
response.render('pages/mapview')
})
//test form submission for single variable entry to db
app.route('/test')
.get(function(request, response){
response.render('pages/test')
})
.post(function(request, response){
var name = request.body.name;
pg.connect(process.env.DATABASE_URL, function(err, client, done){
client.query('INSERT INTO test_table(name) VALUES ($1)', [name], function(err, result) {
done();
if (err)
{ console.error(err); response.send("oops errrrr "); }
else
{ response.redirect('/posts'); } //redirect to sumbissions
});
})
});
//submissions of lost and seen snakes
app.route('/lost')
.get(function(request, response){
response.render('pages/lost')
})
.post(function(request, response){
var species = request.body.snake_species;
var owner = request.body.owner_name;
var snake = request.body.snake_name;
var phone = request.body.contact_phone;
var email = request.body.contact_email;
var lat = request.body.latitude;
var long = request.body.longitude;
pg.connect(process.env.DATABASE_URL, function(err, client, done){
client.query('INSERT INTO lost(snake_species_common, owner_name, snake_name, contact_phone, contact_email, location) VALUES ($1, $2, $3, $4, $5, point($6, $7))',
[species, owner, snake, phone, email, lat, long], function(err, result) {
done();
if (err)
{ console.error(err); response.send("oops errrrr"); }
else
{ response.redirect('/posts'); } //redirect to confirmation of submission
});
})
});
app.route('/seen')
.get(function(request, response){
response.render('pages/seen')
})
.post(function(request, response){
var name = request.body.name;
pg.connect(process.env.DATABASE_URL, function(err, client, done){
client.query('INSERT INTO test_table(name) VALUES ($1)', [name], function(err, result) {
done();
if (err)
{ console.error(err); response.send("oops errrrr"); }
else
{ response.redirect('/sightings'); } //redirect to confirmation of submission
});
})
});
// requests from database for all snake, lost, and sighted tables
app.get('/snake', function (request, response) {
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
if (err)
{ console.error(err); response.send("oops errrrr "); }
client.query('SELECT * FROM snake', function(err, result) {
done();
if (err)
{ console.error(err); response.send("oops errrrr "); }
else
{ response.render('pages/snake', {results: result.rows} ); }
});
});
})
app.get('/sightings', function (request, response) {
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
if (err)
{ console.error(err); response.send("oops errrrr "); }
client.query('SELECT * FROM test_table', function(err, result) {
done();
if (err)
{ console.error(err); response.send("oops errrrr "); }
else
{ response.render('pages/sightings', {results: result.rows} ); }
});
});
})
app.get('/posts', function (request, response) {
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
if (err)
{ console.error(err); response.send("Error " + err); }
client.query('SELECT * FROM lost', function(err, result) {
done();
if (err)
{ console.error(err); response.send("Error " + err); }
else
{ response.render('pages/posts', {results: result.rows} ); }
});
});
})
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});