-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (62 loc) · 1.7 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* 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 bcryptjs = require("bcryptjs")
//Middle ware
// converting json to javascript
app.use(express.json())
/**
* Create an admin user at the starting of the application
* 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 mongo database")
})
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")
}
try{
user = await user_model.create({
name : "Akshat",
userId : "admin",
email : "[email protected]",
userType : "ADMIN",
password : bcryptjs.hashSync("Welcome1",8)
})
console.log("Admin created" , user)
}catch(err){
console.log("Error while creating admin", err)
}
}
/**
* Stich the route to the server
*/
//calling routes and passing app object
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)
})