-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_socket_server.js
More file actions
41 lines (31 loc) · 987 Bytes
/
test_socket_server.js
File metadata and controls
41 lines (31 loc) · 987 Bytes
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
const http = require("http");
const path = require("path");
const fs = require("fs");
function handleRequest(req, res) {
let pathname = req.url
if (pathname === "/") {
pathname = "/index.html"
}
let ext = path.extname(pathname);
const typeExt = {
".html": "text/html",
".js": "text/javascript" ,
"css": "text/css"
};
let contentType = typeExt[ext] || "text/plain"
fs.readFile(__dirname + pathname,
(err, data) => {
if (err) {
res.writeHead(500);
return res.end("Error Loading" + pathname);
}
res.writeHead(200, {"Content-Type": contentType})
res.end(data)
})
}
let server = http.createServer(handleRequest);
server.listen(8080);
let io = require("socket.io").listen(server);
io.on("connection", function(socket) {
console.log("New client logged in!" + socket.id);
});