-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
93 lines (82 loc) · 1.82 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// host this file on glitch
const Chatkit = require("@pusher/chatkit-server");
const chatkit = new Chatkit.default({
instanceLocator: process.env.instanceLocator,
key: process.env.key
});
const express = require("express");
const app = express();
const Pusher = require("pusher");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.get("/users", (req, res) => {
chatkit
.getUsers()
.then(res => {
res.json(res);
})
.catch(err => {
res.send(err);
});
});
app.get("/rooms/joinable", (req, res) => {
chatkit
.getUserJoinableRooms({
userId: req.body.id || "kapil4"
})
.then(result => {
res.send(result);
})
.catch(err => {
res.send(err);
});
});
app.post("/create/user", (req, res) => {
chatkit
.createUser({
id: req.body.id,
name: req.body.name
})
.then(() => {
res.send("User created successfully");
})
.catch(err => {
res.status(500).send(err);
});
});
app.post("/delete/user", (req, res) => {
chatkit
.deleteUser({ userId: req.body.id })
.then(() => {
console.log("User deleted successfully");
res.send("User deleted successfully");
})
.catch(err => {
res.send(err);
});
});
app.post("/create/room", (req, res) => {
chatkit
.createRoom({
creatorId: req.body.id,
name: req.body.name
})
.then(() => {
res.send("Room created successfully");
})
.catch(err => {
res.send(err);
});
});
const port = process.env.PORT || 3000;
app.listen(port, function() {
console.log(`app listening on port ${port}!`);
});