Initialized and setup backend
This commit is contained in:
2
backend/.gitignore
vendored
Normal file
2
backend/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules
|
||||
.env
|
||||
1283
backend/package-lock.json
generated
Normal file
1283
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
21
backend/package.json
Normal file
21
backend/package.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "backend",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"dev": "node --watch ./src/index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.21.1",
|
||||
"mongoose": "^8.8.0",
|
||||
"socket.io": "^4.8.1"
|
||||
}
|
||||
}
|
||||
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