Initialized and setup backend

This commit is contained in:
Manik Maity
2024-11-05 21:12:58 +05:30
parent 50e9a7c6ae
commit 322e7a5b56
6 changed files with 1350 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
import mongoose from "mongoose";
import { DB_URL } from "./veriables.js";
export const connectDB = async () => {
try{
await mongoose.connect(DB_URL);
console.log("Database connected successfully");
}
catch(err){
console.log("Database connection failed");
console.log(err);
}
}

View File

@@ -0,0 +1,5 @@
import dotenv from "dotenv";
dotenv.config();
export const PORT = Number(process.env.PORT);
export const DB_URL = process.env.DB_CONNECTION;

26
backend/src/index.js Normal file
View File

@@ -0,0 +1,26 @@
import express from 'express'
import cors from 'cors'
import { createServer } from 'http'
import { Server } from 'socket.io'
import { PORT } from './config/veriables.js';
import { connectDB } from './config/dbConfig.js';
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: "*"
}
})
app.use(cors())
app.use(express.json())
app.get("/ping", (_req, res) => {
res.json({ message: "pong" })
})
await connectDB();
httpServer.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`)
})