-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (45 loc) · 1.42 KB
/
server.js
File metadata and controls
53 lines (45 loc) · 1.42 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
47
48
49
50
51
52
53
var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('tickets', ['tickets']);
var bodyParser = require('body-parser');
var fs = require('fs');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.get('/tickets', function (req, res) {
console.log('Page updated via GET request');
var data = fs.readFileSync('./db.json');
res.json(JSON.parse(data));
});
app.post('/tickets', function (req, res) {
var data = fs.readFileSync('./db.json');
var json = JSON.parse(data);
console.log(req.body);
json.data.push(req.body);
console.log(json);
var data = fs.writeFileSync('./db.json',JSON.stringify(json));
});
app.delete('/tickets/:id', function (req, res) {
var id = req.params.id;
var data = fs.readFileSync('./db.json');
var json = JSON.parse(data);
json.data.splice(id,1);
console.log(json);
var data = fs.writeFileSync('./db.json',JSON.stringify(json));
});
app.get('/tickets/:id', function (req, res) {
var id = req.params.id;
console.log(id);
});
app.put('/tickets/:id', function (req, res) {
var id = req.params.id;
console.log(id);
console.log(req.body);
var data = fs.readFileSync('./db.json');
var json = JSON.parse(data);
json.data.splice(id, 1, req.body);
console.log(json);
var data = fs.writeFileSync('./db.json',JSON.stringify(json));
});
app.listen(8080);
console.log("Server start at localhost/8080");