aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-07-02 22:54:50 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-07-02 22:54:50 +0200
commit350b7ac055ed6374bde8bf49278c085f583c5781 (patch)
tree1153e6dc043dff134dab3861f42fa19c76836cd1
parent178e900fb843f29c70771226109278451fc15087 (diff)
downloadexample-collection-json-db-350b7ac055ed6374bde8bf49278c085f583c5781.tar.gz
example-collection-json-db-350b7ac055ed6374bde8bf49278c085f583c5781.tar.bz2
example-collection-json-db-350b7ac055ed6374bde8bf49278c085f583c5781.tar.xz
example-collection-json-db-350b7ac055ed6374bde8bf49278c085f583c5781.zip
o Better conneg, defaulting to c+j unless text/html is requested.
-rw-r--r--app.js4
-rw-r--r--package.json1
-rw-r--r--routes/index.js9
3 files changed, 10 insertions, 4 deletions
diff --git a/app.js b/app.js
index dcbfb95..c9e8a1b 100644
--- a/app.js
+++ b/app.js
@@ -1,4 +1,5 @@
-var express = require('express')
+var accept = require('http-accept')
+ , express = require('express')
, routes = require('./routes')
, http = require('http')
, url = require('url');
@@ -14,6 +15,7 @@ app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(urlgenerator);
+ app.use(accept);
app.use(app.router);
app.use(express.static(__dirname + '/public', {maxAge: 60 * 60 * 1000}));
});
diff --git a/package.json b/package.json
index 690b47d..386f76c 100644
--- a/package.json
+++ b/package.json
@@ -8,6 +8,7 @@
"dependencies": {
"collection_json": "git+http://trygvis.dyndns.org/~trygvis/git/2012/06/collection_json.js.git",
"express": "3.0.0beta4",
+ "http-accept": "~0.1.1-2",
"jade": "~0.26.3",
"pg": "0.7.2",
"underscore": "~1.3.3"
diff --git a/routes/index.js b/routes/index.js
index 44feab4..ec57575 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -53,13 +53,15 @@ function after(res, callback) {
}
exports.index = function(req, res) {
- if(req.accepts('html')) {
+ switch(req.accept.types.getBestMatch(["text/html", "application/vnd.collection+json"])) {
+ case "text/html":
res.render('index', {
title: 'Employee DB',
urlgenerator: res.urlgenerator
});
- }
- else {
+ break;
+ case "application/vnd.collection+json":
+ default:
var c = {collection: {
links: [ {
rel: 'departments',
@@ -73,6 +75,7 @@ exports.index = function(req, res) {
}};
res.contentType('application/vnd.collection+json');
res.send(JSON.stringify(collection_json.fromObject(c)), 200);
+ break;
}
};