-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
77 lines (63 loc) · 1.77 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
/**
* This will be the starting file of the project
*/
const express = require("express")
const mongoose = require("mongoose")
const app = express() // Express operator -> function
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()) // Middle ware
/**
* Create an admin user at starting of 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 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 : "Soumyadeep",
userId : "admin",
emailId : "[email protected]",
userType : "ADMIN",
password : bcrypt.hashSync("Your Password Here", 8) // Encrypt password
})
console.log("Admin created", user)
}catch(err) {
console.log("Error while creating admin", err)
}
}
/**
* Stitch the route to server
*/
require("./routes/auth.route")(app)
/**
* Start the server
*/
/**
* MAKE IT CENTRALISED SINCE PORT NO. CAN CHANGE
* app.listen(8080, ()=>{ // 8080 -> Port number
* console.log("Server started")
* })
*/
app.listen(server_config.PORT, ()=>{
console.log("Server started at port number : ", server_config.PORT)
})