forked from learnwithsumit/chat-application
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 13fd52a
Showing
33 changed files
with
4,781 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.env | ||
public/uploads |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"editor.formatOnSave": true, | ||
"[javascript]": { | ||
"editor.formatOnSave": true, | ||
"editor.defaultFormatter": "esbenp.prettier-vscode" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// external imports | ||
const express = require("express"); | ||
const dotenv = require("dotenv"); | ||
const mongoose = require("mongoose"); | ||
const path = require("path"); | ||
const cookieParser = require("cookie-parser"); | ||
const loginRouter = require("./router/loginRouter"); | ||
const usersRouter = require("./router/usersRouter"); | ||
const inboxRouter = require("./router/inboxRouter"); | ||
|
||
// internal imports | ||
const { | ||
notFoundHandler, | ||
errorHandler, | ||
} = require("./middlewares/common/errorHandler"); | ||
|
||
const app = express(); | ||
dotenv.config(); | ||
|
||
// database connection | ||
mongoose | ||
.connect(process.env.MONGO_CONNECTION_STRING, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}) | ||
.then(() => console.log("database connection successful!")) | ||
.catch((err) => console.log(err)); | ||
|
||
// request parsers | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: true })); | ||
|
||
// set view engine | ||
app.set("view engine", "ejs"); | ||
|
||
// set static folder | ||
app.use(express.static(path.join(__dirname, "public"))); | ||
|
||
// parse cookies | ||
app.use(cookieParser(process.env.COOKIE_SECRET)); | ||
|
||
// routing setup | ||
app.use("/", loginRouter); | ||
app.use("/users", usersRouter); | ||
app.use("/inbox", inboxRouter); | ||
|
||
// 404 not found handler | ||
app.use(notFoundHandler); | ||
|
||
// common error handler | ||
app.use(errorHandler); | ||
|
||
app.listen(process.env.PORT, () => { | ||
console.log(`app listening to port ${process.env.PORT}`); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// get inbox page | ||
function getInbox(req, res, next) { | ||
res.render("inbox"); | ||
} | ||
|
||
module.exports = { | ||
getInbox, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// get login page | ||
function getLogin(req, res, next) { | ||
res.render("index"); | ||
} | ||
|
||
module.exports = { | ||
getLogin, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// get users page | ||
function getUsers(req, res, next) { | ||
res.render("users"); | ||
} | ||
|
||
module.exports = { | ||
getUsers, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function decorateHtmlResponse(page_title) { | ||
return function (req, res, next) { | ||
res.locals.html = true; | ||
res.locals.title = `${page_title} - ${process.env.APP_NAME}`; | ||
next(); | ||
}; | ||
} | ||
|
||
module.exports = decorateHtmlResponse; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const createError = require("http-errors"); | ||
|
||
// 404 not found handler | ||
function notFoundHandler(req, res, next) { | ||
next(createError(404, "Your requested content was not found!")); | ||
} | ||
|
||
// default error handler | ||
function errorHandler(err, req, res, next) { | ||
res.locals.error = | ||
process.env.NODE_ENV === "development" ? err : { message: err.message }; | ||
|
||
res.status(err.status || 500); | ||
|
||
if (res.locals.html) { | ||
// html response | ||
res.render("error", { | ||
title: "Error page", | ||
}); | ||
} else { | ||
// json response | ||
res.json(res.locals.error); | ||
} | ||
} | ||
|
||
module.exports = { | ||
notFoundHandler, | ||
errorHandler, | ||
}; |
Oops, something went wrong.