-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (50 loc) · 1.56 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//This will be the starting file of the project
const express = require("express")
const mongoose = require("mongoose")
const app = express()
const server_config = require("./configs/server.config")
const db_config = require("./configs/db.config")
const user_model = require("./models/user.model")
const bcrypt = require("bcryptjs")
app.use(express.json())
//Create an admin user at the starting of the appication if not already present
//Connection with mongodb
mongoose.connect(db_config.DB_URL)
const db = mongoose.connection
db.on("error",()=>{
console.log("Error while connecting to the mongoDB")
})
db.once("open", ()=>{
console.log("Connected to MongoDB")
init()
})
async function init(){
try{
let user = await user_model.findOne({userId : "admin"})
if(user){
console.log("Admin is already present")
return
}
}catch(err){
console.log("Error while reading the data",err)
}
try{
user = await user_model.create({
name : "Shivangi",
userId : "admin",
email : "[email protected]",
userType : "ADMIN",
password : bcrypt.hashSync("Welcome1",8)
})
console.log("Admin created ", user)
}catch(err){
console.log("Error while create admin",err)
}
}
//Stich the route to the server
require("./routes/auth.routes")(app)
require("./routes/category.routes")(app)
//Start the server
app.listen(server_config.PORT, ()=>{
console.log("Server started at port num : ",server_config.PORT)
})