Permalink
Browse files

Adding content negotiation

  • Loading branch information...
bbyars committed Jun 12, 2011
1 parent 64038eb commit 49d9b9540942dc1a44602608a67513ef7121a4b0
Showing with 26 additions and 7 deletions.
  1. +26 −7 server/lib/controlServer.js
@@ -33,6 +33,28 @@ var create = function (port) {
next(); next();
} }
//TODO: Add tests!
function connegRouter(request, response, next) {
var contentTypes = request.headers.accept;
if (contentTypes.indexOf(CONTENT_TYPE) >= 0) {
response.setHeader('Content-type', CONTENT_TYPE);
response.render = function (view, options) {
response.send(options.model);
};
next();
}
else if (contentTypes.indexOf('text/html') >= 0) {
response.setHeader('Content-type', 'text/html');
next();
}
else {
response.setHeader('Accept', CONTENT_TYPE + ', text/html');
response.statusCode = 406;
response.send('Not acceptable.');
}
}
function serverHypermedia(port, response) { function serverHypermedia(port, response) {
return { return {
url: response.absoluteUrl('/', port), url: response.absoluteUrl('/', port),
@@ -95,28 +117,28 @@ var create = function (port) {
app = express.createServer( app = express.createServer(
connect.logger({format: '[ROOT]: :method :url'}), connect.logger({format: '[ROOT]: :method :url'}),
connect.bodyParser(), connect.bodyParser(),
connegRouter,
createAbsoluteUrl); createAbsoluteUrl);
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');
app.listen(port); app.listen(port);
console.log('HTTPMock running at http://localhost:{0}'.format(port)); console.log('HTTPMock running at http://localhost:{0}'.format(port));
app.get('/', function (request, response) { app.get('/', function (request, response) {
response.setHeader('Content-type', CONTENT_TYPE); var hypermedia = {
response.send({
links: [ links: [
{ {
href: response.absoluteUrl('/servers'), href: response.absoluteUrl('/servers'),
rel: response.absoluteUrl('/relations/servers') rel: response.absoluteUrl('/relations/servers')
} }
] ]
}); };
response.send(hypermedia);
}); });
app.get('/servers', function (request, response) { app.get('/servers', function (request, response) {
var result = Object.keys(servers).reduce(function (accumulator, port) { var result = Object.keys(servers).reduce(function (accumulator, port) {
return accumulator.concat(serverHypermedia(port, response)); return accumulator.concat(serverHypermedia(port, response));
}, []); }, []);
response.setHeader('Content-type', CONTENT_TYPE);
response.send({ servers: result }); response.send({ servers: result });
}); });
@@ -129,15 +151,13 @@ var create = function (port) {
server.create(port, function (server) { server.create(port, function (server) {
servers[port] = server; servers[port] = server;
response.setHeader('Content-type', CONTENT_TYPE);
response.setHeader('Location', response.absoluteUrl('/servers/' + port)); response.setHeader('Location', response.absoluteUrl('/servers/' + port));
response.statusCode = 201; response.statusCode = 201;
response.send(serverHypermedia(port, response)); response.send(serverHypermedia(port, response));
}); });
}); });
app.get('/servers/:port', validateServerExists, function (request, response) { app.get('/servers/:port', validateServerExists, function (request, response) {
response.setHeader('Content-type', CONTENT_TYPE);
response.send(serverHypermedia(request.port, response)); response.send(serverHypermedia(request.port, response));
}); });
@@ -155,7 +175,6 @@ var create = function (port) {
results; results;
results = servers[request.port].loadRequests(path); results = servers[request.port].loadRequests(path);
response.setHeader('Content-type', CONTENT_TYPE);
response.send(results); response.send(results);
}); });

0 comments on commit 49d9b95

Please sign in to comment.