-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (63 loc) · 2.11 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
const express = require ('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const yaml = require('js-yaml');
const fs = require('fs');
const pg = require('knex')({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'postgres',
password : '',
database : 'postgres'
}
});
const app = express();
app.use(bodyParser.json());
app.use(cors());
app.get('/profiles', (req, res) => {
pg('profiles').select().orderBy('name')
.then(data => res.json(data))
.catch(err => res.status(500).json(err))
});
app.post('/profiles', (req, res) => {
pg('profiles').insert({name: req.body.name, info: JSON.stringify(req.body)})
.then(data => res.json('success'))
.catch(err => res.status(500).json(err));
});
app.put('/profiles', (req, res) => {
pg('profiles').where('name', '=', req.body.name).update('info', JSON.stringify(req.body))
.then(data => res.json('success'))
.catch(err => res.status(500).json(err));
});
app.delete('/profiles/:name', (req, res) => {
pg('profiles').where('name', req.params.name).del()
.then(data => res.json('success'))
.catch(err => res.status(500).json(err));
});
app.get('/resources', (req, res) => {
pg('resources').select('info').select('protocol').orderBy('name')
.then(data => {
return res.json(data)
})
.catch(err => res.status(500).json(err))
});
app.post('/resources/:protocol', (req, res) => {
pg('resources').insert({name: req.body.name, info: JSON.stringify(req.body), protocol: req.params.protocol})
.then(data => res.json('success'))
.catch(err => res.status(500).json(err));
});
app.put('/resources', (req, res) => {
pg('resources').where('name', '=', req.body.name).update('info', JSON.stringify(req.body))
.then(data => res.json('success'))
.catch(err => res.status(500).json(err));
});
app.delete('/resources/:name', (req, res) => {
pg('resources').where('name', req.params.name).del()
.then(data => {
res.json('success')})
.catch(err => res.status(500).json(err));
});
app.listen(3006, () => {
console.log('app is runing on port 3006')
})