-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
46 lines (36 loc) · 1.13 KB
/
Copy pathserver.js
File metadata and controls
46 lines (36 loc) · 1.13 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
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const { MongoClient} = require('mongodb')
const url = 'mongodb://localhost:27017'
const client = new MongoClient(url)
const dbName = 'companydb'
const collName = 'customers'
app.use(bodyParser.json())
app.use('/', express.static(__dirname + '/dist'))
app.get('/get-posts', async function(req, res){
const db = client.db(dbName)
const collection = db.collection(collName)
const result = await collection.find().toArray()
console.log(result)
res.send(result)
})
app.post('/upload-message', async function(req, res) {
const payload = req.body
console.log(payload)
await client.connect()
console.log("Connected successfully to database.")
//initiate or get the database & collection
const db = client.db(dbName)
const collection = db.collection(collName)
await collection.insertOne(payload)
client.close()
res.send({info: "Message successfully uploaded."})
})
const server = app.listen(3000, function () {
console.log("app listening on port 3000.")
})
module.exports = {
app,
server
}