-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
157 lines (115 loc) · 3.96 KB
/
app.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const http = require('http');
const https = require('https');
const { MongoClient } = require("mongodb");
const server = http.createServer(app);
const io = require('socket.io')(server);
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
const {userJoin,getCurrentUser } = require('./utils/users')
require("dotenv").config();
const uri = process.env.MONGODB_URI;
const apikey = process.env.MEDIASTACK_API_KEY
const client = new MongoClient(uri, { useNewUrlParser: true });
const database = client.db('SampleDatabase');
const collection = database.collection('n');
app.use(bodyParser.urlencoded({ extended: true }));
var article_search;
var eventsObj = {};
var username;
const {login,landingPage,chatRoom, getsearchArticles, articles, postsearchArticles} = require("./handlers")
app.get('/', login)
app.post('/', (req, res) => {
username = req.body.username;
password = req.body.password;
var new_user = {
"username": username,
"events": [{}]
};
res.redirect('/landingPage')
Insert(new_user);
})
app.get('/landingPage', landingPage )
// app.get("/calendar",calendar );
app.get('/chatRoom', chatRoom);
app.post("/calendar", function (req, res) {
inputEvent = req.body.eventInput;
inputDate = req.body.eventDate;
eventsObj = {
date: inputDate,
description: inputEvent
};
if (inputEvent !== "") {
Insert(eventsObj).catch(console.dir);
eventItems.push(inputEvent);
}
res.redirect("/calendar");
})
async function Insert(obj) {
try {
const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient(uri, { useNewUrlParser: true });
// Connect to the client and query
await client.connect();
const collection = client.db("SampleDatabase").collection("n");
// Find the document with "username" equal to username variable
const foundobj = await collection.findOne({ 'username': "Techie"});
// If the document is found, insert the "obj" object into the "events" array
if (foundobj) {
const updatedEvents = [...foundobj.events, obj];
await collection.updateOne({ 'username': username }, { $set: { events: updatedEvents } });
} else {
console.error("No document with username 'Techie' was found");
}
} catch (error) {
console.error("Error while inserting: ", error);
} finally {
// Close the connection to the client
await client.close();
}
}
async function main() {
const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient(uri, { useNewUrlParser: true });
// Connect to the client and query
await client.connect();
var foundobj = collection.findOne({ 'username': username })
foundobj.then(function (doc, e) {
founduser = JSON.stringify(doc)
})
findListings(client, 5);
}
main().catch(console.error);
async function findListings(client, resultsLimit) {
const cursor = client
.db('Calendar')
.collection('Events')
.find()
.limit(resultsLimit);
const results = await cursor.toArray();
if (results.length > 0) {
console.log(`Found ${results.length} Events(s):`);
results.forEach((result, i) => {
resultArr.push(result);
date = new Date(result.last_review).toDateString();
});
}
}
io.on('connection', (socket) => {
console.log("A user is connected");
userJoin(socket.id,username);
socket.on('chat message', (msg,username) => {
var user = getCurrentUser(socket.id);
io.emit('chat message', username + ": " + msg);
});
});
io.on("disconnect",(socket)=>{
console.log("A user is disconnected")
})
app.get('/searchArticles',getsearchArticles)
app.post('/searchArticles', postsearchArticles)
app.get('/articles', articles)
server.listen(3000 )