12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- //to chat: http://codepen.io/anon/pen/RoaJZG?editors=0010
- //http://js.do/code/flexichat
- http = require('http');
- var history = [];
- var messageId = 0;
- server = http.createServer(function(req, res){
- //console.dir(req);
- if (req.method == "POST"){
- var body = '';
- req.on('data', function (data) {
- body += data;
- });
- req.on('end', function () {
- console.log("Body: " + body);
- message = JSON.parse(body);
- var timestamp =(new Date()).getTime();
- if (!("messageId" in message) || (Object.keys(message).length > 1)){
- message.timestamp = timestamp;
- history[messageId] = message;
- messageId++;
- }
- if ("messageId" in message){
- messages = history.slice(+message.messageId);
- // console.log("slice: " + messages.length)
- res.end(JSON.stringify({timestamp: timestamp, messageId: messageId, messages: messages}))
- console.log({timestamp: timestamp, messageId: messageId, messages: messages})
- }
- else {
- res.end(JSON.stringify({timestamp: timestamp, messageId: messageId}))
- console.log({timestamp: timestamp, messageId: messageId})
- }
- });
- res.setHeader('Access-Control-Allow-Origin', '*');
- res.setHeader('Access-Control-Request-Method', '*');
- res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST');
- res.setHeader('Access-Control-Allow-Headers', '*');
- res.writeHead(200, {'Content-Type': 'text/json'});
- //res.end('post received');
- }
- });
- port = 8070;
- host = '164.138.30.21';
- server.listen(port, host);
- console.log("Listen...");
|