-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.js
More file actions
76 lines (63 loc) · 1.68 KB
/
post.js
File metadata and controls
76 lines (63 loc) · 1.68 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
const mysql = require('mysql');
// Get
const con = mysql.createConnection({
host:'localhost',
user:'serveradmin',
password: 'Cm%!35Oa3C7^l',
database:'chat'
});
exports.post = function (req, res){
var values;
var status = 200;
var content = 'text/html';
var body = '';
req.on('data', chunk => {
body += chunk;
});
req.on('end', ()=>{
values = JSON.parse(body);
console.log(values.room);
if(req.url == '/New'){
console.log('Starting posting new Room ' + values.room);
con.query('INSERT INTO rooms (name, url) VALUES(?,?)', [values.room, '/' + values.room],
(err) => {
if(err){
console.log('error while creating entry for room in rooms');
status = 500;
}
else{
console.log('rooms entry successfully created');
}
});
console.log('Creating room table');
con.query('CREATE TABLE IF NOT EXISTS ' + values.room + ' (user VARCHAR(30), text VARCHAR(200), dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)',
req.room,
(err) =>{
if(err){
console.log('error while creating room table');
status = 500;
}
else{
console.log('room table successfully created');
status = 200;
}
});
}
else if(req.url == '/Messages'){
console.log('Request to post to room: ' + values.room + ' with text ' + values.message);
con.query('INSERT INTO ' + values.room + ' (user, text) VALUES(?, ?)',
[values.user, values.message],
(err) => {
if(err){
console.log('error while posting message to rooms');
status = 500;
}
else{
console.log('successfully posted to room');
status = 200;
}
});
}
});
res.writeHead(status, {'Content-Type': content});
}