Created
November 22, 2018 03:43
-
-
Save davidsaccavino/d633b22752e048520e0319f71c762bef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path"); | |
const express = require("express"); | |
const app = express(); | |
const server = require("http").createServer(app); | |
const io = require("socket.io")(server); | |
const port = process.env.PORT || 8080; | |
const mongoose = require("mongoose"), | |
User = require("./user"); | |
require("dotenv").config(); | |
mongoose.connect( | |
`mongodb+srv://oniva:${ | |
process.env.password | |
}@cluster0-uag2f.mongodb.net/test?retryWrites=true` | |
); | |
app.use(express.static(path.join(__dirname, "build"))); | |
app.get("/", (req, res, next) => res.sendFile(__dirname + "index.html")); | |
io.on("connection", client => { | |
client.on("newUser", dict => { | |
new User({ | |
username: `${dict["username"]}`, | |
password: `${dict["password"]}` | |
}).save(function(err) { | |
if (err) console.log(err); | |
}); | |
}); | |
client.on("login", dict => { | |
User.find({ username: dict["username"] }, (err, user) => { | |
if (err) console.log(err); | |
user.comparePassword(dict["password"], function(err, isMatch) { | |
if (err) console.log(err); | |
if (isMatch) { | |
io.emit("loginAccepted", dict["username"]); | |
} | |
}); | |
}); | |
}); | |
client.on("newMsg", dict => { | |
io.emit("message", `${dict["username"]}: ${dict["msg"]}`); | |
}); | |
}); | |
server.listen(port); | |
console.log("Server started at ", port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment