Commit 15a4d7ab authored by Balasuriya D.A.M.'s avatar Balasuriya D.A.M.

Connect Mongo DB to Node js/Express js App

parent eaeedf50
const mongoose = require("mongoose");
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI,{
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline);
} catch (error) {
console.log(`Error: ${error.message}`.red.bold);
process.exit();
}
};
module.exports = connectDB ;
\ No newline at end of file
const express = require("express"); const express = require("express");
const dotenv = require("dotenv"); const dotenv = require("dotenv");
const { chats } = require("./data/data"); const { chats } = require("./data/data");
const connectDB = require("./config/db");
const colors = require("colors");
const app = express();
dotenv.config(); dotenv.config();
connectDB();
const app = express();
app.get("/", (req, res) => { app.get("/", (req, res) => {
res.send("API is Running Successfully"); res.send("API is Running Successfully");
}); });
...@@ -20,6 +28,8 @@ app.get("/api/chat/:id", (req, res) => { ...@@ -20,6 +28,8 @@ app.get("/api/chat/:id", (req, res) => {
res.send(singleChat); res.send(singleChat);
}); });
const PORT = process.env.PORT || 5000; const PORT = process.env.PORT || 5000;
app.listen(5000, console.log(`Server Started on PORT ${PORT}`)); app.listen(5000, console.log(`Server Started on PORT ${PORT}`.yellow.bold));
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment