-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (65 loc) · 2.53 KB
/
index.js
File metadata and controls
74 lines (65 loc) · 2.53 KB
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
// Import Required Modules.====================================================
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
// init module.================================================================
const app = express()
app.use(cors())
app.use(express.json()) // MiddleWare.
// Database connection.========================================================
mongoose.connect('mongodb://localhost:27017/employee')
.then(() => console.log('DB Contected'))
.catch(() => console.log('DB Connection Failed'))
// Difine Mongoose Model and Schema.===========================================
const Employees = mongoose.model('employee', { // Fisrt arg is Collection name.
fullName: { // Second arg is Schema.
type: String,
required: true
},
email: String,
mobile: Number,
city: String
})
// Init Server Connection.=====================================================
const port = process.env.PORT || 3000 // Difine Port Number.
app.listen(port, () => console.log('Server...OK!'))
// Write CRUD Methods.=========================================================
// Reade.------------------------------
app.get('/list', (reqPass, res) => {
Employees.find((err, dbRecords) => {
if (!err) res.send(dbRecords)
else console.log('Error in line Number 36 :', dbRecords)
})
})
// Difine Insert Function--------------
function create(req, resPass) {
var jsObject = new Employees() // Use Line Number 17 var to make new Object
jsObject.fullName = req.body.fullName
jsObject.email = req.body.email
jsObject.mobile = req.body.mobile
jsObject.city = req.body.city
// console.log(jsObject)
jsObject.save((err, docsPass) => { //
if (err) console.log('Error in line Number 49 :', err)
})
}
// update Function.--------------------
function update(req, resPass) {
if (req.body.fullName) {
Employees.findByIdAndUpdate({ _id: req.body._id }, req.body, (err, docsPass) => {
if (err) console.log('Error in line Number 57 :', err)
})
}
}
// Post for create or update.
app.post('/', (req, res) => {
if (!req.body._id) create(req, res)
else update(req, res)
})
// Delete.-----------------------------
app.delete('/delete/:id', (req, res) => {
Employees.findByIdAndDelete({ _id: req.params.id }, (err, docsPass) => {
if (err) console.log('Error in line Number 71 :', err)
else res.send('')
})
})