-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (48 loc) · 1.62 KB
/
app.js
File metadata and controls
59 lines (48 loc) · 1.62 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
/**
* Created by peter on 4/01/16.
*/
var port = 8001;
var http = require('http');
var url = require('url');
var fs = require('fs');
var io = require('socket.io');
var pounchdb=require('pouchdb');
var i = 0;
var server = http.createServer(function (request, response) {
var path = url.parse(request.url).pathname;
switch (path) {
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('Hi this is from Nawfal with request number =' + (i++));
response.end();
break;
case '/socket.html':
fs.readFile(__dirname + path, function (error, data) {
if (error) {
response.writeHead(404);
response.write('Opps, this doesnt exist - 404');
response.end();
} else {
response.writeHead(200, {"Content-type:": "text/html"});
response.write(data, "utf8");
response.end();
}
});
break;
default:
response.writeHead(404);
response.write('Opps, this doesnt exist - 404');
response.end();
break;
}
// console.log(request);
});
server.listen(port);
var listerner = io.listen(server);
listerner.sockets.on('connection', function (socket) {
// console.log('We got a connection');
socket.emit('message', {'message': 'Hi this is Nawfal from Socket IO and the time right now is :' + Date.now()});
socket.on('client_data', function (data) {
console.log(data.letter);
});
});