-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
174 lines (136 loc) · 4.98 KB
/
app.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/***********************
Load Components!
Express - A Node.js Framework
Body-Parser - A tool to help use parse the data in a post request
Pug - A view engine for dynamically rendering HTML pages
Pg-Promise - A database tool to help use connect to our PostgreSQL database
***********************/
const express = require('express'); // Add the express framework has been added
const {Client} = require('pg');
const client = new Client({
connectionString: "postgres://cfbopxmzjrchow:72765cb500d3817ba5706c05b7a01f9e5d9777b211307e272ee71c658722c0cc@ec2-54-221-214-183.compute-1.amazonaws.com:5432/dduosdsl53eu2b",
ssl: true,
});
client.connect();
let app = express();
const bodyParser = require('body-parser'); // Add the body-parser tool has been added
app.use(bodyParser.json()); // Add support for JSON encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // Add support for URL encoded bodies
//Create Database Connection
const pgp = require('pg-promise')();
/**********************
Database Connection information
host: This defines the ip address of the server hosting our database. We'll be using localhost and run our database on our local machine (i.e. can't be access via the Internet)
port: This defines what port we can expect to communicate to our database. We'll use 5432 to talk with PostgreSQL
database: This is the name of our specific database. From our previous lab, we created the football_db database, which holds our football data tables
user: This should be left as postgres, the default user account created when PostgreSQL was installed
password: This the password for accessing the database. You'll need to set a password USING THE PSQL TERMINAL THIS IS NOT A PASSWORD FOR POSTGRES USER ACCOUNT IN LINUX!
**********************/
// REMEMBER to chage the password
/*
const dbConfig = {
host: 'ec2-54-221-214-183.compute-1.amazonaws.com',
port: 5432,
database: 'dduosdsl53eu2b',
user: 'cfbopxmzjrchow',
password: '72765cb500d3817ba5706c05b7a01f9e5d9777b211307e272ee71c658722c0cc'
};
*/
//let db = pgp(dbConfig);
// set the view engine to ejs
app.set('view engine', 'pug');
app.use(express.static(__dirname + '/')); // This line is necessary for us to use relative paths and access our resources directory
app.get('/', (req,res)=>{
return res.redirect('/HomePage/home');
});
app.get('/HomePage/home', function(req, res) {
res.render('/HomePage/home',{
my_title:"Home Page"
});
});
app.get('/LoginPage/login', function(req, res) {
res.render('/LoginPage/login',{
});
});
//go to home
//grab password associated to email if it exists
app.post('/LoginPage/login', function(req,res){
console.log(req.body);
res.render('/profile/profile');
/*
var loginEmail = req.body.email;
var loginPassword = req.body.password;
//res.render('pages/HomePage/home');
var query = "select * from user_info where '"+loginEmail+"'=email AND '"+loginPassword+"'=password";
//query for questions join on user_info
//query for answers join on user_info
db.any(query)
.then(function(rows){
res.render('/Profile/profile',{
my_title: "Profile",
data: rows,
})
console.log(rows);
})
.catch(function (err){
request.flash('error', err);
res.render('/LoginPage/login',{
my_title: "Login Page",
data: '',
})
})
*/
});
app.get('/LoginPage/signup', function(req, res) {
res.render('/LoginPage/signup',{
local_css:"signup.css",
my_title:"Signup Page"
});
});
app.post('/LoginPage/signup', function(req,res){
//console.log('req.body');
console.log("HEYYYA");
res.render('/LoginPage/signup');
client.query("INSERT INTO user_info(firstname,lastname,email,password) VALUES('"+req.body.firstName+"','"+req.body.lastName+"','"+req.body.email+"','"+req.body.password+"');", function(err,res){
if(err) throw err;
});
});
app.get('/Profile/profile', function(req,res){
res.render('/Profile/profile',{
my_title: "Profile Page"
});
});
app.get('/QuestionPage/question', function(req,res){
res.render('/QuestionPage/question', {
local_css: "question.css",
my_title: "Question"
});
});
app.post('/QuestionPage/question', function(req,res){
console.log(req.body);
res.render('/ViewQuestion/viewquestion'); //posts question and redirects
//inserts question into query
client.query("INSERT INTO questions(question_ask, question_info, question_tag, user_email) VALUES('"+req.body.title+"','"+req.body.details+"','"+req.body.tag+"','"+req.body.email+"');", function (err,res){
if(err) throw err;
});
});
//match with title and topic for query
app.get('/SearchPage/search', function(req,res){
res.render('/SearchPage/search',{
my_title: "Search"
});
});
app.get('/SearchPage/resultPage',function(req,res){
res.render('/SearchPage/resultPage',{
my_title: "Search Result"
});
});
app.get('/ViewQuestion/viewquestion', function(req,res){
res.render('/ViewQuestion/viewquestion', {
my_title: "View Question "
});
});
app.listen(3000, function(err){
if(err) throw err;
console.log('3000 is the magic port');
});