-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
69 lines (58 loc) · 2 KB
/
router.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
const { Router } = require('express')
const mongo = require('./mongo')
const fs = require('fs')
require('dotenv').config()
const DB_NAME = process.env.DB_NAME
function buildConnection() {
return mongo.getClient()
}
async function buildRouter(client) {
let router = new Router()
let db = await mongo.getDatabase(client, DB_NAME);
let targetCollection = await mongo.getCollection(db, process.env.TARGET_COLLECTION)
router.get('/', (req, res) => res.sendFile(__dirname + "index.html"))
router.get('/ping', (req, res) => res.status(200).send('Server is up.'))
router.route('/test')
.get((req, res) => {
targetCollection.find({ "name": req.query.name })
.toArray()
.then(result => {
res.status(200)
.json({ "result" : result })
})
})
.post((req, res) => {
targetCollection.insertOne(req.body)
.then(result => {
res.status(200)
.json({ "result": result})
}).catch((err) => {
res.status(400)
.json({ "error": err})
})
})
router.route('/postTest')
.post((req, res) => {
let request = req.body
if (request.hasOwnProperty('message')) {
res.status(200)
.json({ message: "Thanks"})
} else {
res.status(400)
.json({ message: "Please send json with 'message' key"})
}
})
router.get('/message', (req, res) => {
res.status(200).json({ "message": "Hello!" })
})
router.get('/data', (req, res) => {
let rawData = fs.readFileSync('./csvjson.json')
let parsedData = JSON.parse(rawData)
res.json(parsedData.data)
})
return router
}
module.exports = {
buildConnection: buildConnection,
buildRouter: buildRouter
}