-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
123 lines (92 loc) · 3.08 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// This is node server.js where we are creating Node API and the backend express.js running port on 9000
// by creating our api we needs to do importing
import express from "express";
import mongoose from "mongoose";
import Messages from "./dbMessages.js"
import Pusher from "pusher";
import cors from "cors";
// const express = require("express");
// Then app configuration
const app = express();
const port = process.env.PORT || 9000;
// const Pusher = require("pusher");
const pusher = new Pusher({
appId: "1210090",
key: "72ea0bfa631cd8c38fa9",
secret: "a38de11a8027b038682b",
cluster: "eu",
useTLS: true
});
pusher.trigger("my-channel", "my-event", {
message: "hello world"
});
// next working in middleware
app.use(express.json());
app.use(cors());
// As cors package has been installed so we had comment out this code below
// app.use((req, res, next) => {
// res.setHeader("Access-Control-Allow-Origin", "*");
// res.setHeader("Access-Control-Allow-Headers", "*");
// next();
// });
// DataBase(DB) configuration(This will connect with our mongodb database)
const connection_url =
'mongodb+srv://matesappadmin:E6gWsJzQ989hwuME@cluster0.sa5xl.mongodb.net/matesappdb?retryWrites=true&w=majority'
mongoose.connect(process.env.MONGODB_URI || connection_url,{
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection
db.once("open", () => {
console.log("DB has been CONNECTED!!!");
const msgCollection = db.collection("messagecontents")
// console.log(msgCollection);
const changeStream = msgCollection.watch();
changeStream.on("change",(change) => {
// console.log(change);
console.log("A change has been occured!", change);
if (change.operationType === "insert") {
const messageDetails = change.fullDocument;
pusher.trigger("messages", "inserted",
{
name: messageDetails.name,
message: messageDetails.message,
timestamp: messageDetails.timestamp,
received: messageDetails.received,
});
} else {
console.log("Errors by triggering Pusher");
}
});
});
// ??????
// API routes (GET/POST/DELETE)
app.get("/",(req,res)=>res.status(200).send('hello nodejs world'))
app.get("/messages/sync", (req, res) => {
Messages.find((err, data) => {
if (err) {
res.status(500).send(err);
} else {
res.status(201).send(data);
}
});
});
// app.post("/api/v1/messages/new", (req, res) => {
app.post("/messages/new", (req, res) => {
const dbMessage = req.body
Messages.create(dbMessage, (err, data) => {
if (err) {
res.status(500).send(err);
} else {
// res.status(201).send(`new messages created: \n ${data}`)
res.status(201).send(data);
}
});
});
// STEP 3 DEPLOYMENT
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
// Listen
app.listen(port,()=>console.log(`Listening on localhost:${port}`));