-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (41 loc) · 1.35 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
/**
* Created by konoe_mario on 2017/07/18.
*/
var http = require("http");
var server = http.createServer();
//urlディスパッチャに使う
var url = require("url");
//htmlファイルの読み込み
var fs = require("fs");
server.on("request",function(req, res){
console.log("リクエストがあったよ");
var incomingUrl = url.parse(req.url);
console.log(incomingUrl.pathname);
if(incomingUrl.pathname === "/controller"){
fs.readFile("./client/controller.html","utf-8",(err,data)=> {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
res.write('<head><meta charset="UTF-8"></head>');
res.end("<h1>Not Found</h1>");
}else{
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
}
});
}else{
res.writeHead(404,{'Content-Type':'text/html'});
res.write('<head><meta charset="UTF-8"></head>');
res.end("<h1>Not Found</h1>");
}
});
//socket.ioで使う
var io = require("socket.io")(server);
//socket確認用のコード
io.sockets.on("connection",function (socket) {
console.log("socket connection");
socket.on("sendMessage",function (data) {
socket.emit('test',data);
});
});
server.listen(6677);