Initialized and setup backend
This commit is contained in:
13
backend/src/config/dbConfig.js
Normal file
13
backend/src/config/dbConfig.js
Normal 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);
|
||||
}
|
||||
}
|
||||
5
backend/src/config/veriables.js
Normal file
5
backend/src/config/veriables.js
Normal 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
26
backend/src/index.js
Normal 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}`)
|
||||
})
|
||||
Reference in New Issue
Block a user