forked from heroku/node-js-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (129 loc) · 5 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
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
const cool = require('cool-ascii-faces')
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const cors = require('cors')
const validator = require('validator')
const PORT = process.env.PORT || 5000
const sample_cars =
[
{
id : "5cdf411856e9c200042989d7",
username : "JANET",
lat : 42.354951,
lng : -71.0509,
created_at : "2020-05-17T23:17:44.427Z"
},
{
id : "5cf583aafbbfe80004456918",
username : "mXfkjrFw",
lat : 42.3453,
lng : -71.0464,
created_at : "2020-06-03T20:31:38.378Z"
},
{
id : "5cf583aafbbfe80004456919",
username : "nZXB8ZHz",
lat : 42.3662,
lng : -71.0621,
created_at : "2020-06-03T20:31:38.611Z"
},
{
id : "5cf583aafbbfe8000445691a",
username : "Tkwu74WC",
lat : 42.3603,
lng : -71.0547,
created_at : "2020-06-03T20:31:38.786Z"
}
]
const { Client } = require('pg');
const client = new Client({
connectionString: process.env.DATABASE_URL || "postgres://nodepsqlapp_user:abc123@localhost:5432/nodepsqlapp",
ssl: {
rejectUnauthorized : false,
}
});
client.connect();
express()
.use(express.static(path.join(__dirname, 'public')))
.use(bodyParser.urlencoded())
.use(cors())
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/cool', (req, res) => res.send(cool()))
.post("/rides", cors(), (req, res) => {
if (!req.body.username || !req.body.lat || !req.body.lng) {
res.send('{"error":"Whoops, something is wrong with your data!"}');
}
else {
// TODO: send the error message above if lat, lng aren't floats
// for now, want to adhere to the spec's conditions re when to send the error response
if (validator.isFloat(req.body.lat) && validator.isFloat(req.body.lng)) {
client.query('INSERT INTO riders VALUES ($1, $2, $3)',
[req.body.username, req.body.lat, req.body.lng], (error, result) => {
console.log("psql query sent, inserting into 'riders'");
// TODO: What's the field for the actual error message text?
if (error) console.log("there was an error finding rides:", error);
else console.log("rider added to db");
});
} else {
console.log("lat and lng aren't floats");
}
client.query('SELECT * FROM drivers', (error, result) => {
if (!error) {
res.send(JSON.stringify(result.rows));
}
else {
console.log("There was an issue fetching available rides from the database.");
res.send(JSON.stringify(sample_cars));
}
});
}
})
.post("/offerride", cors(), (req, res) => {
if (!req.body.username || !req.body.lat || !req.body.lng ||
!validator.isFloat(req.body.lat) || !validator.isFloat(req.body.lng)) {
res.send('{"error":"Whoops, something is wrong with your data!"}');
}
client.query('INSERT INTO drivers VALUES ($1, $2, $3)',
[req.body.username, req.body.lat, req.body.lng], (error, result) => {
console.log("psql query sent, inserting into 'drivers'");
if (error) console.log("there was an error offering a ride:", error);
else console.log("driver added to db");
});
res.send("Thank you for offering a ride!!");
})
.post("/passenger.json", cors(), (req, res) => {
if (!req.body.username) res.send(JSON.stringify([]));
else {
client.query('SELECT * FROM riders WHERE username = $1', [req.body.username], (error, result) => {
if (error) res.send('{"error" : "There was an error retreiving the user\'s ride requests."}');
else res.send(JSON.stringify(result.rows));
});
}
})
.post("/vehicle.json", cors(), (req, res) => {
if (!req.body.username) res.send(JSON.stringify([]));
else {
client.query('SELECT * FROM drivers WHERE username = $1', [req.body.username], (error, result) => {
if (error) res.send('{"error" : "There was an error retreiving the user\'s ride offers."}');
else res.send(JSON.stringify(result.rows));
});
}
})
.get("/", (req, res) => {
let indexPage = '<!DOCTYPE html><html lang="en"><head><title>Catch A Ride!</title></head>';
indexPage += '<body><h1>People are currently offering rides from:</h1>';
client.query('SELECT * FROM drivers', (error, result) => {
if (!error) {
for (var count = 0; count < result.rows.length; count++) {
indexPage += '<p>' + result.rows[count].username + ' is offering a ride from (';
indexPage += result.rows[count].lat.toString() + ', ' + result.rows[count].lng.toString();
indexPage += ')</p>';
}
}
indexPage += '</body></html>';
res.send(indexPage);
});
})
.listen(PORT, () => console.log(`Listening on ${ PORT }`))