-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbunserver.js
More file actions
63 lines (57 loc) · 1.58 KB
/
bunserver.js
File metadata and controls
63 lines (57 loc) · 1.58 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
60
61
62
63
//TEST
//does not work since socket and different req and rep. bun use fetch style.
//bun 1.1
var public_path = "public"; //folder
function fetch(req, server) {
const success = server.upgrade(req);//websocket
console.log("success: ",success)
if (success) {
// Bun automatically returns a 101 Switching Protocols
// if the upgrade succeeds
return undefined;
}
//console.log('URL: ' + req.url)
let filePath = new URL(req.url).pathname;
console.log('path: ' + filePath);
if (filePath == '/') {
filePath = public_path+'/index.html'
const file = Bun.file(filePath);
return new Response(file);
} else {
if(filePath == "/favicon.ico"){
}
filePath = public_path + filePath;
const file = Bun.file(filePath);
return new Response(file);
}
}
const server = Bun.serve({
fetch: fetch,
// https://bun.sh/guides/websocket/pubsub
// https://bun.sh/guides/websocket/compression
websocket: {
// this is called when a message is received
async message(ws, message) {
console.log(`Received ${message}`);
// send back a message
//ws.send(`You said: ${message}`);
},
open(ws) {
console.log("ws open")
}, // a socket is opened
close(ws, code, message) {
console.log("ws close")
}, // a socket is closed
drain(ws) {
console.log("ws drain")
}, // the socket is ready to receive more data
},
port: 1337
})
console.log('Server is running on port 1337!')
//console.log(server)
// const gun = Gun({
// web: server
// })
//console.log(gun);
console.log(`Listening on http://${server.hostname}:${server.port}`)