-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (76 loc) · 2.25 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
var http = require('http');
var fs = require('fs');
var mysql = require('mysql');
var qs = require('querystring');
var DBhost = 'myinstance.cbkhb0ifzpmr.us-east-1.rds.amazonaws.com';
var username = 'admin';
var pass = '12345678';
var db = 'TempDB';
http.createServer(function (req, res) {
if (req.method === 'GET' && req.url === '/') {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile ('index.html', function(err, data) {
res.write(data);
res.end();
}
);
}
else if (req.method === 'GET' && req.url === '/status') {
res.writeHead(200, {'Content-Type': 'application/json'});
var answer = { endpoint: req.url, clientIP: req.connection.remoteAddress };
res.write(JSON.stringify(answer));
res.end();
}
else if (req.method === 'GET' && req.url === '/item') {
res.writeHead(200, {'Content-Type': 'application/json'});
var con = mysql.createConnection({
host: DBhost,
user: username,
password: pass,
database: db
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM Items", function (err, result, fields) {
if (err) throw err;
res.write(JSON.stringify(result));
res.end();
});
});
}
else if (req.method === 'POST' && req.url === '/item') {
var postData = '';
req.on('data', function(content) {
postData += content;
if (postData.length > 1e6) {
request.connection.destroy();
}
});
req.on('end', function() {
var payload = qs.parse(postData);
res.writeHead(200, {'Content-Type': 'text/plain'});
var con = mysql.createConnection({
host: DBhost,
user: username,
password: pass,
database: db
});
con.connect(function(err) {
if (err) throw err;
con.query("INSERT INTO Items(name) VALUES ('" + payload.name + "')", function (err, result, fields) {
if (err) throw err;
res.write('1 record was inserted!');
res.end();
});
});
//var itemName = payload.name;
//res.write(itemName);
//res.end();
});
}
//}
else {
res.statusCode = 404;
res.end();
}
}).listen(80);