From 350b7ac055ed6374bde8bf49278c085f583c5781 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 2 Jul 2012 22:54:50 +0200 Subject: o Better conneg, defaulting to c+j unless text/html is requested. --- app.js | 4 +++- package.json | 1 + routes/index.js | 9 ++++++--- 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; } }; -- cgit v1.2.3