Skip to content

Commit

Permalink
added db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
abutler911 committed Oct 16, 2023
1 parent 9b1a25e commit 5839d4d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
require("dotenv").config();
const express = require("express");
const path = require("path");
const connectDB = require("./config/db");
const indexRoutes = require("./routes/indexRoutes");

const app = express();
const port = 3000;

// Connect to MongoDB
connectDB();

// Setting EJS as the view engine
app.set("view engine", "ejs");

// Setting up the public folder to serve static files
app.use(express.static(path.join(__dirname, "public")));

app.get("/", (req, res) => {
// Render an EJS view file
res.render("index", { message: "Hello World!" });
});
// Use routes
app.use("/", indexRoutes);

app.listen(port, () => {
console.log(`Emotional Journey App listening on http://localhost:${port}`);
Expand Down
16 changes: 16 additions & 0 deletions config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require("mongoose");

const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Successfully connected to MongoDB...");
} catch (error) {
console.error("Error connecting to MongoDB", error);
process.exit(1);
}
};

module.exports = connectDB;
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "MIT",
"dependencies": {
"bcrypt": "^5.1.1",
"dotenv": "^16.3.1",
"ejs": "^3.1.9",
"express": "^4.18.2",
"express-session": "^1.17.3",
Expand Down
8 changes: 8 additions & 0 deletions routes/indexRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require("express");
const router = express.Router();

router.get("/", (req, res) => {
res.render("index", { message: "Hello World!" });
});

module.exports = router;

0 comments on commit 5839d4d

Please sign in to comment.