-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
126 lines (107 loc) · 3.91 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
// requirements // npm install nodemon TO MAKE LIFE EASIER
const express = require('express'); // HAVE TO npm install express
const bodyParser = require('body-parser');
const mysql = require('mysql'); // HAVE TO npm install mysql
const identify = require('./password.js')
// constants
const app = express();
const port = 80;
// middleware
const urlEncodedParser = bodyParser.urlencoded({extended: false});
const jsonParser = app.use(bodyParser.json());
// set static path ()
app.use(express.static("public"));
// get
app.get("/", function(req, res, next) {
app.sendfile(path.join(__dirname, "assets/index.html"));
});
// post
app.post('/send', function(req, res) {
database.query('insert into blogentries (title, content, datetime) value(\"'+ req.body.title + '\", \"' + req.body.blogpost + '\", CURRENT_TIMESTAMP);',
function(error){
if (error) {throw error};
});
res.json("success");
});
app.post('/openingPage', function(req, res){
database.query('SELECT * FROM blogentries', (error, data) => {
if (error) {throw error};
postdata = data;// how to make something the response.
database.query('SELECT * FROM comments', (error, data) => {
if (error) {throw error};
commentdata = data;// how to make something the response.
completeData = {
postbody: postdata,
commentbody: commentdata
}
res.json(completeData);
});
});
});
app.post('/delete', function(req, res){
let sql = 'delete from blogentries where postId=' + req.body.identification;
database.query(sql, (error, data) => {
if (error) {throw error};
let sql2 = 'delete from comments where mainPostId=' + req.body.identification;
database.query(sql, (error, data) => {
if (error) {throw error};
res.json('response');
})
})
})
app.post('/deleteComment', function(req, res){
let sql = 'delete from comments where commentId=' + req.body.identification;
database.query(sql, (error, data) => {
if (error) {throw error};
res.json('response');
})
})
app.post('/comment', function(req, res) {
let sql = 'insert into comments (mainPostId, content, datetime) value("'+ req.body.mainPostId + '", "' + req.body.comment + '", CURRENT_TIMESTAMP);'
database.query(sql, function(error){
if (error) {throw error};
});
res.json("success");
})
app.post('/submitEditPost', function(req, res) {
database.query('update blogentries set title="' + req.body.title +'" where postId="' + req.body.identification +'";',
function(error, data){
if (error) {throw error};
database.query('update blogentries set content="' + req.body.content +'" where postId="' + req.body.identification +'";',
function(error, data){
if (error) {throw error};
res.json(data);
});
});
});
app.post('/submitEditComment', function(req, res) {
database.query('update comments set content="' + req.body.content +'" where commentId="' + req.body.identification +'";',
function(error, data){
if (error) {throw error};
res.json(data);
});
});
//------------------------------------------------------------------------------
// server start
app.listen(port, function() {
console.log("Server started...");
});
//------------------------------------------------------------------------------
// mysql
// connection!
var database = mysql.createConnection({
host: 'localhost', // ip address to the host of the mysql database
user: identify.user, // the thing in ____@_______ (the user to access/edit database)
password: identify.password,
database: 'blog'
});
// commence connection
database.connect(function(error){ // what happens when theres an error????
if (error) {throw error};
console.log('Connected!');
database.query('use blog', function (error, result){ // puts "use blog" in
if (error) {throw error}; // the terminal
console.log("Result: " + result);
});
});
//------------------------------------------------------------------------------