Permalink
Browse files

Easier http with Express

  • Loading branch information...
robjoh committed Aug 18, 2014
1 parent 1ebd6fb commit 9a01d5d03951f34a0c06aa189ab786cbde7d0c98
Showing with 23 additions and 26 deletions.
  1. +2 −1 package.json
  2. +21 −25 server.js
View
@@ -21,6 +21,7 @@
},
"homepage": "https://github.com/robjoh/azure-service-bus-nodejs",
"dependencies": {
"azure": "~0.6.9-zumo"
"azure": "~0.6.9-zumo",
"express": "~4.8.4"
}
}
View
@@ -3,37 +3,33 @@
var namespace = 'azure-service-bus-nodejs',
accessKey = '[Access key for this namespace]',
serviceBus = require('./service-bus.js'),
http = require('http');
http = require('http'),
express = require('express');
var client = serviceBus.createClient(namespace, accessKey);
var app = express();
var server = http.createServer(function(httpReq, httpResp) {
if (httpReq.url === '/') {
client.getTopics(writeResult);
} else {
var parts = httpReq.url.split('/');
if (parts.length === 2) {
client.getSubscriptions(parts[1], writeResult);
} else {
client.getSubscription(parts[1], parts[2], writeResult);
}
}
function writeResult(error, result) {
if (error) {
httpResp.writeHead(500, {'Content-Type': 'application/json'});
httpResp.write(JSON.stringify(error, null, 3));
httpResp.end();
return;
}
app.get('/', function(httpReq, httpResp) {
client.getTopics(writeResult(httpResp));
});
httpResp.writeHead(200, {'Content-Type': 'application/json'});
httpResp.write(JSON.stringify(result, null, 3));
httpResp.end();
}
app.get('/:topic', function(httpReq, httpResp) {
client.getSubscriptions(httpReq.params.topic, writeResult(httpResp));
});
server.listen(8080);
app.get('/:topic/:subscription', function(httpReq, httpResp) {
client.getSubscription(httpReq.params.topic, httpReq.params.subscription, writeResult(httpResp));
});
http.createServer(app).listen(8080);
function writeResult(httpResp) {
return function(error, result) {
if (error) {
httpResp.status(500).send(error);
return;
}
httpResp.status(200).send(result);
}
}

0 comments on commit 9a01d5d

Please sign in to comment.