-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·75 lines (58 loc) · 1.71 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
var express = require('express');
var port = process.env.PORT || 3000;
var websiteRoot = __dirname + '/public/';
var bodyParser = require('body-parser');
var app = express();
var middleWare = require('./middleware.js');
var qaItemController = require('./qaItemController.js');
app.use(middleWare.logger);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }) );
// webserver root
app.get('/', function(req, res) {
res.sendFile(websiteRoot + 'index.html');
});
// public website
app.use(express.static(websiteRoot));
// RESTful web services
// Read
app.get('/qas', function(req, res) {
var qaItems;
if (req.query && req.query.category) {
qaItems = qaItemController.getCategory(req.query.category);
} else {
qaItems = qaItemController.getAll();
}
res.json(qaItems);
});
// Create
app.post('/qas', function(req, res) {
// todo validatiion
var qaItem = qaItemController.qaItemCreate(req.body);
res.json(qaItem);
});
// Delete
app.delete('/qas/:id', function(req, res) {
var qaDeletedItem;
if (req.params) {
console.log(req.params);
qaDeletedItem = qaItemController.deleteQA(req.params.id);
}
res.json(qaDeletedItem);
});
// Update
app.put('/qas', function(req, res) {
qaUpdatedItem = qaItemController.updateQA(req.body)
res.json(qaUpdatedItem);
});
app.post('/formtest',
function(req, res) {
console.log(req.body);
var qaItem = qaItemController.qaItemCreate(req.body);
res.json(qaItem);
});
app.listen(port, function() {
console.log("Listening to requests at port " + port)
});
// TODO: authentication
// app.use(middleWare.requestAuthentication);