forked from apiacademy/ndcoslo2015
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
147 lines (123 loc) · 3.69 KB
/
app.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************
* task service implementation
* service implementation (server)
* May 2015
* Mike Amundsen (@mamund)
* Soundtrack : Complete Collection : B.B. King (2008)
*******************************************************/
// handles routing, conneg, response-representation
// base modules
var http = require('http');
var querystring = require('querystring');
/// internal modules
var representation = require('./representor.js');
// connector modules
var home = require('./connectors/home.js');
var utils = require('./connectors/utils.js');
// shared vars
var root = '';
var port = (process.env.PORT || 8181);
var jsonType = 'application/json';
var cjType = 'application/vnd.collection+json';
var uberType = 'application/vnd.uber+json';
var haljsonType = 'application/vnd.hal+json';
var repjsonType = 'application/representor+json';
var sirenType = 'application/vnd.siren+json';
var htmlType = "text/html";
var jsonUrlType = "application/json;profile=urls";
var jsonFormType = "application/json;profile=forms";
var csType = '';
var csAccept = '';
// routing rules
var reFile = new RegExp('^\/files\/.*','i');
var reHome = new RegExp('^\/.*','i');
// request handler
function handler(req, res) {
var segments, i, x, parts, rtn, flg, doc, url;
// set local vars
root = '//'+req.headers.host;
csType = cjType;
flg = false;
file = false;
doc = null;
// rudimentary accept-header handling
csAccept = req.headers["accept"];
if(!csAccept || csAccept.indexOf(cjType)!==-1) {
csType = cjType;
}
else {
csType = csAccept.split(',')[0];
}
// TK: this forces the request type
//csType = sirenType;
// parse incoming request URL
parts = [];
segments = req.url.split('/');
for(i=0, x=segments.length; i<x; i++) {
if(segments[i]!=='') {
parts.push(segments[i]);
}
}
// handle options call
if(req.method==="OPTIONS") {
sendResponse(req, res, "", 200);
return;
}
// file handler
try {
if(flg===false && reFile.test(req.url)) {
flg = true;
utils.file(req, res, parts, handleResponse);
}
}
catch(ex) {}
// home handler
if(reHome.test(req.url)) {
flg = true;
doc = home(req, res, parts, handleResponse);
}
// final error
if(flg===false) {
handleResponse(req, res, utils.errorResponse(req, res, 'Not Found', 404));
}
}
// handle response work
function handleResponse(req, res, doc) {
var rtn;
if(doc!==null) {
if(doc.file===true) {
rtn = doc.doc;
}
else {
rtn = representation(doc.doc, csType, root);
}
sendResponse(req, res, rtn, doc.code, doc.headers);
}
else {
sendResponse(req, res, 'Server Response Error', 500);
}
}
function sendResponse(req, res, body, code, headers) {
var hdrs;
if(headers && headers!==null) {
hdrs = headers;
}
else {
hdrs = {}
}
if(!hdrs['content-type']) {
hdrs['content-type'] = csType;
}
// always add CORS headers to support external clients
hdrs["Access-Control-Allow-Origin"] = "*";
hdrs["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
hdrs["Access-Control-Allow-Credentials"] = true;
hdrs["Access-Control-Max-Age"] = '86400'; // 24 hours
hdrs["Access-Control-Allow-Headers"] = "X-Requested-With, Access-Control-Allow-Origin, X-HTTP-Method-Override, Content-Type, Authorization, Accept";
res.writeHead(code, hdrs),
res.end(body);
}
// wait for request
http.createServer(handler).listen(port);
console.log('listening on port '+port);
// EOF