-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
89 lines (66 loc) · 2.15 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express');
const app = express();
const port = 3000;
const mysql = require("mysql");
const connection = mysql.createConnection({
host: 'localhost',
user: 'aydin',
password: 'zegikniet',
database: 'molveno'
});
connection.connect( (err) => {
if (err) {
throw err;
} else {
console.log('connected to database!');
}
});
// import body-parser module here
let bodyParser = require('body-parser');
// say to the app (express instance) that he might sometimes render
// the body of a POST/PUT from JSON to an Object
app.use(bodyParser.json());
// for now this is to say that everyone can reach this webserver
// from everywhere
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/api/guest', (req, res) => {
res.setHeader('Content-Type', 'application/json');
connection.query('SELECT * FROM guest', (err, guests) => {
if(err) throw err;
res.send(guests);
});
});
app.listen(port, () => {
console.log('Server running on port: ', port);
});
app.post('/api/guest', function (req, res) {
let content = req.body;
connection.query('INSERT INTO guest SET ?', content, (err, result) => {
if (err) throw err;
res.send(result)
});
});
app.delete('/api/guest/:id', function(req, res) {
let id = +req.params.id;
connection.query('DELETE FROM guest WHERE id = ?', id, (err, result) => {
if(err) throw err;
console.log('Deleted ', result.affectedRows,' rows');
res.status(204).end();
});
});
app.put('/api/guest/:id', function(req, res) {
let id = +req.params.id;
let inputUser = req.body;
connection.query('UPDATE guest SET ? WHERE id = ?', [inputUser, id], (err, response) => {
if(err) throw err;
connection.query('SELECT * FROM guest WHERE id = ?', id, (updatedErr, updatedGuest) => {
if(updatedErr) throw updatedErr;
res.send(updatedGuest);
});
});
});