-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (37 loc) · 1.02 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
'use strict';
var mysql = require('mysql');
var con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'calories',
timezone: 'utc'
});
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.use(bodyParser.json());
app.use(express.static('client'));
var db = require('./db_queries');
var dbQueries = db(con);
app.get('/meals', function(req, res) {
dbQueries.listMeals(req, function (result) {
res.send(result);
});
});
app.post('/meals', urlencodedParser, function(req, res) {
dbQueries.addOneMeal(req.body, function (result) {
res.send(result);
});
});
app.delete('/meals/:id', urlencodedParser, function(req, res) {
dbQueries.deleteOneMeal(req.params.id, function (result) {
if (result.affectedRows === 1) {
res.send({status: 'ok', id: req.params.id})
} else {
res.send({status: 'not exists'});
}
});
});
app.listen(3000);