-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
75 lines (61 loc) · 2.13 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
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const { MongoClient } = require('mongodb')
const { isInvalidEmail, isEmptyPayload } = require('./validator')
const { DB_USER, DB_PASS, DEV } = process.env
const dbAddress = '127.0.0.1:27017'
const url = DEV ? `mongodb://${dbAddress}` : `mongodb://${DB_USER}:${DB_PASS}@${dbAddress}?authSource=company_db`
const client = new MongoClient(url)
const dbName = 'company_db'
const collName = 'employees'
app.use(bodyParser.json())
app.use('/', express.static(__dirname + '/dist'))
app.get('/get-profile', async function(req, res) {
// connect to mongodb database
await client.connect()
console.log('Connected successfully to server')
// initiate or get the db & collection
const db = client.db(dbName)
const collection = db.collection(collName)
// get data from database
const result = await collection.findOne({id: 1})
console.log(result)
client.close()
response = {}
if (result !== null) {
response = {
name: result.name,
email: result.email,
interests: result.interests
}
}
res.send(response)
})
app.post('/update-profile', async function(req, res) {
const payload = req.body
console.log(payload)
if (isEmptyPayload(payload) || isInvalidEmail(payload)) {
res.send({error: "invalid payload. Couldn't update user profile data"})
} else {
// connect to mongodb database
await client.connect()
console.log('Connected successfully to database server')
// initiate or get the db & collection
const db = client.db(dbName)
const collection = db.collection(collName)
// save payload data to the database
payload['id'] = 1
const updatedValues = { $set: payload }
await collection.updateOne({id: 1}, updatedValues, {upsert: true})
client.close()
res.send({info: "user profile data updated successfully"})
}
})
const server = app.listen(3000, function () {
console.log("app listening on port 3000")
})
module.exports = {
app,
server
}