diff options
-rw-r--r-- | app.js | 4 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | routes/index.js | 9 |
3 files changed, 10 insertions, 4 deletions
@@ -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; } }; |