forked from aalonsog/BSDT_rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_server.js
68 lines (57 loc) · 1.84 KB
/
rest_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
var express = require('express');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var path = require('path');
var model = require('./model');
var app = express();
var port = 8000;
model.init(function() {
console.log('RESTful Service listening in port', port);
app.listen(port);
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride('_method', {methods: ['GET', 'POST']}));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.send('Restaurants REST Service\n');
})
app.get('/restaurants', function(req, res) {
model.list(req.query, function (restaurants) {
res.status(200).send(restaurants);
}, function (error) {
res.status(500).send(error);
});
});
app.get('/restaurants/:restaurantId', function(req, res) {
model.read(req.params.restaurantId, function (restaurant) {
res.status(200).send(restaurant);
}, function (error) {
res.status(500).send(error);
});
});
app.post('/restaurants', function(req, res) {
model.create(req.body, function (restaurants) {
res.status(200).send('Created');
}, function (error) {
res.status(500).send(error);
});
});
app.put('/restaurants/:restaurantId', function(req, res) {
model.update(req.params.restaurantId, req.body, function (restaurants) {
res.status(200).send('Updated');
}, function (error) {
res.status(500).send(error);
});
});
app.delete('/restaurants/:restaurantId', function(req, res) {
model.delete(req.params.restaurantId, function (restaurants) {
res.status(200).send('Deleted');
}, function (error) {
res.status(500).send(error);
});
});
// handle 404 errors
app.use(function(req, res){
res.status(404).send('Not found');
});