This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
executable file
·117 lines (102 loc) · 3.46 KB
/
service.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Init
var restify = require('restify');
var server = restify.createServer({
name: 'datepickerContacts'
});
server.use(restify.queryParser());
server.use(restify.bodyParser());
// Configure routes
var PATH = '/contacts';
server.get({path: PATH, version: '0.0.1'}, getAllContacts);
server.get({path : PATH + '/:contactId' , version : '0.0.1'} , getContact);
server.post({path: PATH, version: '0.0.1'}, addContact);
server.put({path: PATH + '/:contactId', version: '0.0.1' }, updateContact);
server.del({path : PATH +'/:contactId' , version: '0.0.1'}, deleteContact);
server.opts({path: PATH + '/:contactId', version: '0.0.1' }, crossOrigin);
server.opts({path: PATH, version: '0.0.1' }, crossOrigin);
// Data. As long as this REST-service stays alive, we can append a simple variable with data.
var contacts = [
{ id: 1, firstName: 'Frank', surname: 'Muscles', email: 'frank@muscles.com', editMode: false, isInvited : false},
{ id: 2, firstName: 'Eddy', surname: 'Valentino', email: 'eddy@valfam.co.uk', editMode: false, isInvited : false }
];
// For testing errorhandling, let's throw an error every ten requests
var requestCount = 0;
server.pre(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
requestCount++;
//if(requestCount === 10) {
// requestCount = 0;
// console.error('-- Returning an internal server error to test errorhandling.');
// res.send(500, 'An almost randomly selected internal server error.');
// return;
//}
return next();
});
// Start the server
server.listen(9688, '127.0.0.1', function () {
console.log('%s listening at %s ', server.name, server.url);
});
// Dealing with CORS
function crossOrigin(req,res,next){
var allowHeaders = ['Accept', 'Accept-Version', 'Content-Type', 'Api-Version', 'Origin', 'X-Requested-With']; // added Origin & X-Requested-With
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Headers', allowHeaders.join(', '));
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
//res.header('Access-Control-Allow-Origin', '*');
if(req.method === 'OPTIONS') {
res.send(204);
return;
}
return next();
}
// The REST functions
function getAllContacts(req, res, next) {
console.log('GET: all contacts');
res.send(200, contacts);
return next();
}
function getContact(req, res, next) {
console.log('GET: One contact by ID', req.params.contactId);
for(var i = 0; i < contacts.length; i++) {
if(contacts[i].id === req.params.contactId) {
res.send(200, contacts[i]);
return;
}
}
}
function addContact(req, res, next) {
console.log('POST: Add contact', req.params);
var contact = {
id: contacts.length + 1,
firstName: req.params.firstName,
surname: req.params.surname,
email: req.params.email
};
contacts.push(contact);
res.send(201, contact);
return next();
}
function updateContact(req, res, next) {
console.log('PUT: Update contact by ID', req.params.contactId, req.params);
for(var i = 0; i < contacts.length; i++) {
if(contacts[i].id === parseInt(req.params.contactId)) {
contacts[i].firstName = req.params.firstName;
contacts[i].surname = req.params.surname;
contacts[i].email = req.params.email;
break;
}
}
res.send(204);
return next();
}
function deleteContact(req, res, next) {
console.log('DELETE: Delete contact by ID', req.params.contactId);
for(var i = 0; i < contacts.length; i++) {
if(contacts[i].id === parseInt(req.params.contactId)) {
contacts.splice(i, 1);
break;
}
}
res.send(204);
return next();
}