-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (64 loc) · 1.75 KB
/
index.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
require("dotenv").config();
const Express = require("express");
const BodyParser = require("body-parser");
const app = Express();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
const MongoClient = require("mongodb").MongoClient;
const uri = process.env.URL;
const DATABASE_NAME = process.env.DB_NAME;
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));
var collection;
var count = 0;
app.post("/push", (request, response) => {
console.log(request.body.username);
collection.insert(request.body, (error, result) => {
if (error) {
return response.status(500).send(error);
}
response.send(result.result);
});
});
app.get("/get-all", (request, response) => {
collection.find({}).toArray((error, result) => {
if (error) {
return response.status(500).send(error);
}
response.send(result);
});
});
app.put("/edit-name/", (request, response) => {
collection.update(
{ username: request.body.username },
{
$set: {
fullName: request.body.fullName,
},
}
);
response.status(200).send("Update successful");
});
io.on("connection", (socket) => {
console.log("a user connected");
count++;
var username = `User${count}: `;
socket.on("disconnect", () => {
console.log("user disconnected");
});
socket.on("chat message", (msg) => {
console.log(msg);
io.emit("chat message", username + msg);
});
});
app.listen(4000, () => {
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect((err) => {
if(err) {
return;
}
collection = client.db(DATABASE_NAME).collection("users");
// perform actions on the collection object
console.log("Connected to " + DATABASE_NAME);
});
});