-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
137 lines (108 loc) · 3.67 KB
/
app.js
File metadata and controls
137 lines (108 loc) · 3.67 KB
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
let express = require('express'),
bodyParser = require('body-parser'),
exphbs = require('express-handlebars'),
// ? для чего использовать Pool
{ Pool, Client } = require('pg'),
app = express();
// LOCAL version
const USER = {
// name and password - при входе в pgAdmin3
name: 'postgres',
password: 'test123',
// Локальные данные
host: 'localhost',
port: '5432',
// db - имя БД с которой работаю
db: 'flights',
// table - таблица с которой работаю, расположена в db
table: 'flights1'
}
// SERVER version
const USER_SERVER = {
// name and password - все данные с Heroku
name: 'wwagzkrthsjslb',
password: 'f037a36e6e2452e351f9e88446df480d013dcc943b5de972a461682fb8811dd9',
// Локальные данные
host: 'ec2-54-217-235-159.eu-west-1.compute.amazonaws.com',
port: '5432',
// db - имя БД с которой работаю
db: 'd6u19tp8jutgfb',
// table - таблица с которой работаю, расположена в db
table: 'flights1'
}
// Соединение с БД
// Local
// const connect = `postgresql://${USER.name}:${USER.password}@${USER.host}:${USER.port}/${USER.db}`;
// Server Heroku
const connect = `postgres://${USER_SERVER.name}:${USER_SERVER.password}@${USER_SERVER.host}:${USER_SERVER.port}/${USER_SERVER.db}`;
app.use(express.static(__dirname + '/public'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
// Корень сайта
app.get('/', function(req, res){
// ? Разница в использовании Pool и Client
// const pool = new Pool({
// connectionString: connect,
// });
// pool.query('SELECT * FROM flights', (err, res2) => {
// res.render('home', {temp: res2.rows, test: 'hello'});
// pool.end();
// });
const client = new Client({
connectionString: connect,
ssl: true
});
client.connect();
// Использую res2 чтобы не было конфликта имен
client.query(`SELECT * FROM ${USER.table}`, (err, res2) => {
// test - можно удалять
res.render('home', {temp: res2.rows, test: 'hello'});
client.end();
});
});
// Добовляю пост (капитан :))
app.post('/add', function(req, res) {
const client = new Client({
connectionString: connect,
ssl: true
});
client.connect();
client.query(`INSERT INTO ${USER.table}(origin, destination, duration) VALUES($1, $2, $3)`,
[req.body.origin, req.body.destination, req.body.duration], (err, res2) => {
res.redirect('/');
client.end();
});
});
// Удаляю пост (снова капитан :))
app.delete('/delete/:id', function(req, res) {
const client = new Client({
connectionString: connect,
ssl: true
});
client.connect();
client.query(`DELETE FROM ${USER.table} WHERE id = $1`,
[req.params.id], (err, res2) => {
client.end();
res.sendStatus(200); // ?
});
});
// Редактирую пост
app.post('/edit', function(req, res) {
const client = new Client({
connectionString: connect,
ssl: true
});
client.connect();
client.query(`UPDATE ${USER.table} SET origin=$1, destination=$2, duration=$3 WHERE id = $4`,
[req.body.origin, req.body.destination, req.body.duration, req.body.id], (err, res2) => {
res.redirect('/');
client.end();
});
});
// При загрузке через Heroku
// Приложение включается через другой порт: process.env.PORT
app.listen(process.env.PORT || 8080, function(){
console.log('http://localhost:8080');
});