Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ian Halverson's solution to project_superchat #7

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store

node_modules
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# project_superchat
Build a realtime multi-room chat application. Make it super.

Names: Ian Halverson and Ed Triplett
71 changes: 71 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io")(server);
const exphbs = require("express-handlebars");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const redis = require("redis");
const redisClient = redis.createClient();
const {
getAllData,
newMessage,
createRoom
} = require("./services/redis_handler");

app.engine("handlebars", exphbs({ defaultLayout: "main" }));
app.set("view engine", "handlebars");

app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(__dirname + "/public/"));

app.use(
"/socket.io",
express.static(__dirname + "/node_modules/socket.io-client-dist/")
);

app.get("/", (req, res) => {
getAllData().then(data => {
res.render("index.handlebars", {
data,
name: req.cookies.user || "Anonymous"
});
});
});

app.post("/name", (req, res) => {
let userName = req.body.name;
res.cookie("user", userName);
res.redirect("/");
});

app.post("/clear", (req, res) => {
res.cookie("user", "Anonymous");
res.redirect("/");
});

io.on("connection", client => {
client.on("created room", room => {
createRoom(room).then(() => {
io.emit("room created");
});
});

client.on("newMessage", data => {
let returnData = data;
let room = data[2];
let user = data[0];
let message = data[1];
newMessage(room, user, message).then(() => {
io.emit("message saved", returnData);
if (message.includes("super") || message.includes("Super")) {
io.emit("activateSuperBot", room);
}
});
});
});

server.listen(3000, () => {
console.log(`Listening on localhost:3000`);
});
Loading