aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-07-04 14:12:13 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-07-04 14:12:13 +0200
commit3d6f13430471ef3948c57510d39e8ce80352046d (patch)
tree2d4df1ab4f07f183c3e518836da39705297a5705
parentc95f92fa4c42ef86cd994ca0361cb3c1909e6f2d (diff)
downloadexample-collection-json-db-3d6f13430471ef3948c57510d39e8ce80352046d.tar.gz
example-collection-json-db-3d6f13430471ef3948c57510d39e8ce80352046d.tar.bz2
example-collection-json-db-3d6f13430471ef3948c57510d39e8ce80352046d.tar.xz
example-collection-json-db-3d6f13430471ef3948c57510d39e8ce80352046d.zip
o Common and proper HEAD vs GET handling.
-rw-r--r--routes/index.js110
1 files changed, 53 insertions, 57 deletions
diff --git a/routes/index.js b/routes/index.js
index ba2ca23..8e4c28f 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -54,29 +54,53 @@ function after(res, callback) {
function method(handlers) {
return function(req, res) {
+ // I'm just too lazy to include two rows
+ handlers.HEAD = handlers.GET;
var handler = handlers[req.method];
if(handler) {
return handler(req, res)
}
- // I'm just too lazy to include two rows
- if(req.method == 'HEAD') {
- var handler = handlers[req.method];
- if(handler) {
- return handler(req, res)
- }
- }
res.header('Allow', _.keys(handlers));
return res.send(405);
}
}
+function send_html(req, res, render) {
+ if(req.method == 'HEAD') {
+ // Can't be bothered to calculate Content-Length even if I
+ // should..
+ res.writeHead(200, {
+ 'Content-Type': 'text/html',
+ });
+ res.end();
+ }
+ else {
+ render();
+ }
+}
+
+function send_cj(req, res, c) {
+ var text = JSON.stringify(collection_json.fromObject(c));
+ var headers = {
+ 'Content-Type': 'application/vnd.collection+json',
+ 'Content-Length': text.length
+ };
+ if(req.method == 'HEAD') {
+ res.writeHead(200, headers);
+ res.end();
+ }
+ else {
+ res.send(text, headers, 200);
+ }
+}
+
function getIndex(req, res) {
switch(req.accept.types.getBestMatch(['text/html', 'application/vnd.collection+json'])) {
case 'text/html':
- res.render('index', {
+ send_html(req, res, _.bind(res.render, res, 'index', {
title: 'Employee DB',
urlgenerator: res.urlgenerator
- });
+ }));
break;
case 'application/vnd.collection+json':
case '*/*':
@@ -91,8 +115,7 @@ function getIndex(req, res) {
href: res.urlgenerator.employees()
} ]
}};
- res.contentType('application/vnd.collection+json');
- res.send(JSON.stringify(collection_json.fromObject(c)), 200);
+ send_cj(req, res, c);
break;
default:
res.send(406);
@@ -113,11 +136,11 @@ function getDepartments(req, res) {
client.query(sql2, [ p.offset, p.limit ], after(res, function(rs2) {
switch(req.accept.types.getBestMatch(['text/html', 'application/vnd.collection+json'])) {
case 'text/html':
- res.render('departments', {
+ send_html(req, res, _.bind(res.render, res, 'departments', {
title: 'Department List',
urlgenerator: res.urlgenerator, pager: p, query: req.query,
departments: rs2.rows
- });
+ }));
break;
case 'application/vnd.collection+json':
case '*/*':
@@ -125,8 +148,7 @@ function getDepartments(req, res) {
href: res.urlgenerator.departments(),
items: mapDepartments(res, rs2.rows)
}};
- res.contentType('application/vnd.collection+json');
- res.send(JSON.stringify(collection_json.fromObject(c)), 200);
+ send_cj(req, res, c);
break;
default:
res.send(406);
@@ -144,11 +166,11 @@ function getDepartment(req, res) {
var dept_no = req.params.dept_no;
switch(req.accept.types.getBestMatch(['text/html', 'application/vnd.collection+json'])) {
case 'text/html':
- res.render('department', {
+ send_html(req, res, _.bind(res.render, res, 'department', {
title: 'Department ' + dept_no,
urlgenerator: res.urlgenerator,
dept_no: dept_no
- });
+ }));
break;
case 'application/vnd.collection+json':
case '*/*':
@@ -164,8 +186,7 @@ function getDepartment(req, res) {
href: res.urlgenerator.departments()
} ]
}};
- res.contentType('application/vnd.collection+json');
- res.send(JSON.stringify(collection_json.fromObject(c)), 200);
+ send_cj(req, res, c);
break;
default:
res.send(406);
@@ -190,12 +211,12 @@ function getEmployeesInDepartment(req, res) {
client.query(sql2, [ dept_no, p.offset, p.limit ], after(res, function(rs2) {
switch(req.accept.types.getBestMatch(['text/html', 'application/vnd.collection+json'])) {
case 'text/html':
- res.render('employeesInDepartment', {
+ send_html(req, res, _.bind(res.render, res, 'employeesInDepartment', {
title: 'Department: #' + dept_no,
urlgenerator: res.urlgenerator, pager: p, query: req.query,
dept_no: dept_no,
employees: rs2.rows
- });
+ }));
break;
case 'application/vnd.collection+json':
case '*/*':
@@ -208,8 +229,7 @@ function getEmployeesInDepartment(req, res) {
} ],
items: mapEmployees(res, rs2.rows)
}};
- res.contentType('application/vnd.collection+json');
- res.send(JSON.stringify(collection_json.fromObject(c)), 200);
+ send_cj(req, res, c);
break;
default:
res.send(406);
@@ -224,8 +244,6 @@ exports.employeesInDepartment = method({
});
function getEmployees(req, res) {
- var head = req.method == 'HEAD';
-
pg.connect(process.env.DATABASE_URL, function(err, client) {
if(err) throw err;
var emp_no = req.params.emp_no;
@@ -257,22 +275,12 @@ function getEmployees(req, res) {
client.query(sql2, sql2Params, after(res, function(rs2) {
switch(req.accept.types.getBestMatch(['text/html', 'application/vnd.collection+json'])) {
case 'text/html':
- if(head) {
- // Can't be bothered to calculate Content-Length even if I
- // should..
- res.writeHead(200, {
- 'Content-Type': 'text/html',
- });
- res.end();
- }
- else {
- res.render('employees', {
- title: 'Employee List',
- urlgenerator: res.urlgenerator, pager: p, query: req.query,
- employees: rs2.rows,
- query: req.query
- });
- }
+ send_html(req, res, _.bind(res.render, res, 'employees', {
+ title: 'Employee List',
+ urlgenerator: res.urlgenerator, pager: p, query: req.query,
+ employees: rs2.rows,
+ query: req.query
+ }));
break;
case 'application/vnd.collection+json':
case '*/*':
@@ -305,18 +313,7 @@ function getEmployees(req, res) {
}],
items: mapEmployees(res, rs2.rows)
}};
- var text = JSON.stringify(collection_json.fromObject(c));
- var headers = {
- 'Content-Type': 'application/vnd.collection+json',
- 'Content-Length': text.length
- };
- if(head) {
- res.writeHead(200, headers);
- res.end();
- }
- else {
- res.send(text, headers, 200);
- }
+ send_cj(req, res, c);
break;
default:
res.send(406);
@@ -338,11 +335,11 @@ function getEmployee(req, res) {
client.query(sql, [ emp_no ], after(res, function(rs) {
switch(req.accept.types.getBestMatch(['text/html', 'application/vnd.collection+json', '*/*'])) {
case 'text/html':
- res.render('employee', {
+ send_html(req, res, _.bind(res.render, res, 'employee', {
title: 'Employee: #' + emp_no,
urlgenerator: res.urlgenerator,
employee: rs.rows[0]
- });
+ }));
break;
case 'application/vnd.collection+json':
case '*/*':
@@ -350,8 +347,7 @@ function getEmployee(req, res) {
href: res.urlgenerator.employee(emp_no),
items: [ mapRow(rs.rows[0]) ],
}};
- res.contentType('application/vnd.collection+json');
- res.send(JSON.stringify(collection_json.fromObject(c)), 200);
+ send_cj(req, res, c);
break;
default:
res.send(406);