-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (25 loc) · 836 Bytes
/
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
'use strict'
const { Database, Server } = require('./src/Server')
const server = new Server()
const database = new Database(server.config)
server.post('/', (request, response) => {
const { command, options } = request.body
const [ type, operation ] = command.split('-')
switch (type) {
case 'collection':
database.use(options.database)
try {
database.getCollection(options.collection)[operation](options.data)
.then((result) => {
server.write(response, JSON.stringify(result))
}).catch((error) => {
console.log(error)
})
} catch (e) {
// TODO Manage the error in a proper way
server.write(response, JSON.stringify({message: 'Unexpected error.'}))
}
break
}
})
server.start()