Skip to content

Commit 5e69b9b

Browse files
committed
init
0 parents  commit 5e69b9b

File tree

8 files changed

+1138
-0
lines changed

8 files changed

+1138
-0
lines changed

.gitignore

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# TypeScript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+
.env.test
60+
61+
# parcel-bundler cache (https://parceljs.org/)
62+
.cache
63+
64+
# next.js build output
65+
.next
66+
67+
# nuxt.js build output
68+
.nuxt
69+
70+
# vuepress build output
71+
.vuepress/dist
72+
73+
# Serverless directories
74+
.serverless/
75+
76+
# FuseBox cache
77+
.fusebox/
78+
79+
# DynamoDB Local files
80+
.dynamodb/

api.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const express = require("express");
2+
const bodyParser = require("body-parser");
3+
const app = express();
4+
const http = require("http").Server(app);
5+
const io = require("socket.io")(http);
6+
const models = require('./models');
7+
const dbConnection = require('./dbconnection');
8+
9+
var server = require('./server')
10+
11+
app.use(bodyParser.json());
12+
app.use(bodyParser.urlencoded({ extended: false }));
13+
app.use(express.static(__dirname));
14+
15+
let Chats = models.ChatsModel;
16+
17+
// Connect to the database
18+
dbConnection.Connect();
19+
server.init();
20+
21+
app.post("/chats", async (req, res) => {
22+
try {
23+
let chat = new Chats(req.body);
24+
await chat.save();
25+
res.sendStatus(202).send(chat._id);
26+
io.emit("chat", req.body);
27+
} catch (error) {
28+
res.sendStatus(500);
29+
console.error(error);
30+
}
31+
});
32+
33+
// Post request on chats endpoind with ID parameter
34+
app.post("/chats/:id", async (req, res) => {
35+
try {
36+
let chatsFound;
37+
let chat = Chats.find({_id: req.params.id}, (error, chats) => {
38+
chatsFound = chats;
39+
});
40+
if(chat){
41+
// We found a chat with the supplied ID
42+
let user1 = search(chatsFound.user1ID, connections);
43+
let user2 = search(chatsFound.user2ID, connections);
44+
// Are both users currently online?
45+
if( user1 && user2 ){
46+
// Then broadcast the message over sockets
47+
socket.broadcast.to(user1.socketID).emit('chat', req.body);
48+
socket.broadcast.to(user2.socketID).emit('chat', req.body);
49+
}
50+
}
51+
await Chats.findByIdAndUpdate(
52+
req.params.id,
53+
req.body,
54+
(err, chat) => {
55+
if(err) return res.status(500).send(err);
56+
return res.send(chat);
57+
}
58+
)
59+
} catch (error) {
60+
res.sendStatus(500)
61+
console.error(error)
62+
}
63+
});
64+
65+
app.get("/chats", (req, res) => {
66+
Chats.find({}, (error, chats) => {
67+
res.send(chats)
68+
});
69+
});
70+
71+
app.get("/chats/:id", (req, res) => {
72+
Chats.find({ _id: req.params.id}, (error, chats) => {
73+
console.log(req.params.id);
74+
console.log(chats);
75+
res.sendStatus(202).send(chats);
76+
res.send(chats)
77+
});
78+
});
79+
80+
app.delete("/chats/:id", (req, res) => {
81+
Chats.deleteOne({ _id: req.params.id}, (error, chats) => {
82+
if(error){
83+
return res.status(error.statusCode).send({
84+
message: res.statusCode
85+
});
86+
}
87+
return res.status(202);
88+
});
89+
});
90+
91+
process.on('exit', () => {
92+
dbConnection.Disconnect();
93+
});
94+
95+
function search(nameKey, myArray){
96+
for (var i=0; i < myArray.length; i++) {
97+
if (myArray[i].name === nameKey) {
98+
return myArray[i];
99+
}
100+
}
101+
}
102+

dbconnection.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const mongoose = require("mongoose");
2+
const conString = "mongodb://test:[email protected]:45923/huizingdb";
3+
4+
function Connect(){
5+
mongoose.connect(conString, {useNewUrlParser: true}, (err) => {
6+
if(err){
7+
throw(err);
8+
}
9+
console.log("Database connection established")
10+
});
11+
}
12+
13+
function Disconnect(){
14+
mongoose.disconnect();
15+
}
16+
17+
module.exports.Connect = Connect;
18+
module.exports.Disconnect = Disconnect;

index.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
<title>Epic, epic ftw.</title>
3+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" crossorigin="anonymous">
4+
<script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous"></script>
5+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" crossorigin="anonymous"></script>
6+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" crossorigin="anonymous"></script>
7+
<script src="/socket.io/socket.io.js"></script>
8+
9+
<script>
10+
var socket = io()
11+
socket.on("chat", addChat)
12+
13+
$(() => {
14+
console.log('getting chats');
15+
getChats();
16+
17+
$("#send").click(() => {
18+
var chatMessage = {
19+
name: $("#txtName").val(), chat: $("#txtMessage").val()
20+
}
21+
postChat(chatMessage)
22+
})
23+
})
24+
function postChat(chat) {
25+
$.post("http://localhost:4001/chats", chat)
26+
}
27+
28+
function getChats() {
29+
$.get("/chats", (chats) => {
30+
chats.forEach(addChat)
31+
})
32+
}
33+
function addChat(chatObj){
34+
$("#messages").append(`<h5>${chatObj.name} </h5><p>${chatObj.chat}</p>`);
35+
}
36+
37+
</script>
38+
39+
<div class="container">
40+
<br>
41+
<div class="jumbotron">
42+
<h1 class="dispaly-4">Chat</h1>
43+
<br>
44+
<input id="txtName" class="form-control" placeholder="Name" type="text">
45+
<br>
46+
<textarea id="txtMessage" class="form-control" placeholder="Message"></textarea>
47+
<br>
48+
<button id="send" class="btn btn-success">Send</button>
49+
</div>
50+
<div id="messages"></div>
51+
</div>

models.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const mongoose = require("mongoose");
2+
3+
const Message = new mongoose.Schema({
4+
message: String,
5+
from: String,
6+
});
7+
8+
const ChatSchema = new mongoose.Schema({
9+
user1ID: String,
10+
user2ID: String,
11+
messages: [Message]
12+
});
13+
14+
var Chats = mongoose.model("Chats", ChatSchema);
15+
16+
exports.ChatsModel = Chats;
17+
exports.Message = Message;
18+
exports.ChatSchema = ChatSchema;

0 commit comments

Comments
 (0)