-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_http_node.js
75 lines (63 loc) · 2.31 KB
/
test_http_node.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
/*
* This work has been done by Phillip Ochola Makanyengo
* Email: [email protected]
*
* This work uses open source code and libraries and
* can therefore be replicated unless certain portions
* are stated otherwise.
*
* Please refer to the author when using the code.
*
*/
//Nodejs implementation of a http server
// Import required Modules
var comms = require('morphbridge').comms,
logger = require('morphbridge').logger,
channels_obj = require('morphbridge').channels_obj,
url = require('url'),
buffer = channels_obj.newBuffer(), //Buffer to store messages
http = require('http');
//Optionally Set a limit to the buffer to limit the size
//buffer.setLimit(5); //This code limits the buffer to 20 messages
//Handle internode messages
/*
Place your own function to handle messages recieved by the node.
*/
var handle = function(msg){
//console.log('HTTP node received ZMQ message');
//logger.logStat('HTTP node received ZMQ message');
buffer.load(msg);
};
//Socket Initialisation
comms.init(handle); //Pass message handling function to sub_socket
// Create a Server
http.createServer(function (req, res) {
console.log("We got another one!! \n");
//Get data from request and commit to global pool
var queryObject = url.parse(req.url,true).query;
console.log(queryObject[0]);
if (queryObject[0]) comms.transmit(queryObject[0]);
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://41.242.2.202:800'); //Change this ip to yours.. sry.. :D
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
if (buffer.buffer.length > 0){
//console.log(buffer.unload().toString());
res.writeHead(200, {'Content-Type': 'text/html'});
res.end("'"+buffer.unload().toString()+"'");
}
else{
res.writeHead(200, {'Content-Type': 'text/html'});
res.end("No messages yet!");
}
}).listen(8084);
process.on('SIGINT', function() {
comms.close()
console.log('\nClosed')
process.exit();
});