Permalink
Browse files

Added the initial files for the node.js axon-addressbook sample. You …

…can request a list of all contacts.
  • Loading branch information...
1 parent dd133b3 commit 0c312e20c005ee8f16235c984cabcfc335a60fbf @jettro committed Jun 30, 2011
Showing with 150 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +17 −0 ContactController.js
  3. +40 −0 ContactRepository.js
  4. +25 −0 app.js
  5. +13 −0 package.json
  6. +18 −0 public/js/jquery-1.6.1.min.js
  7. +12 −0 test/TestContactRepository.js
  8. +9 −0 views/index.jade
  9. +14 −0 views/layout.jade
View
@@ -0,0 +1,2 @@
+/.idea/
+node_modules/
View
@@ -0,0 +1,17 @@
+/**
+ *
+ * @author Jettro Coenradie
+ */
+var repository;
+
+function ContactController(contactsRepository) {
+ repository = contactsRepository;
+}
+
+ContactController.prototype.listContacts = function(req,res) {
+ repository.listContacts(function(contacts) {
+ res.render('index', {locals: {contacts: contacts}});
+ });
+};
+
+module.exports = ContactController;
View
@@ -0,0 +1,40 @@
+/**
+ * Repository object that handles all requests to the backend.
+ *
+ * @author Jettro Coenradie
+ */
+var http = require('http');
+var host = 'localhost';
+var port = 8080;
+
+function ContactRepository() {}
+
+ContactRepository.createRepo = function(host, port) {
+ var repo = new ContactRepository();
+ repo.host = host;
+ repo.port = port;
+ return repo;
+};
+
+ContactRepository.prototype.listContacts = function(callback) {
+ var opts = createHttpRequestOpts('/contacts','GET');
+
+ var req = http.get(opts, function(res){
+ res.on('data', function(data) {
+ callback(JSON.parse(data));
+ });
+ });
+
+};
+
+function createHttpRequestOpts(path, method) {
+ return {
+ host: host,
+ port: port,
+ path:path,
+ headers:{'Accept':'application/json','Content-Type':'application/json'},
+ method: method
+ };
+}
+
+module.exports = ContactRepository;
View
25 app.js
@@ -0,0 +1,25 @@
+/**
+ *
+ * @author Jettro Coenradie
+ */
+var pub = __dirname + '/public';
+var ContactController = require('./ContactController');
+var repository = require('./ContactRepository').createRepo('localhost',8080);
+var contactController = new ContactController(repository);
+
+var express = require('express')
+ , app = express.createServer();
+
+app.configure(function() {
+ app.set('view engine', 'jade');
+ app.set('views', __dirname + '/views');
+ app.set('view options', { layout: 'layout' });
+ app.use(express.methodOverride());
+ app.use(express.static(pub));
+ app.use(express.bodyParser());
+});
+
+app.get('/', contactController.listContacts);
+
+app.listen(8018);
+console.log('Express server started on port %s', app.address().port);
View
@@ -0,0 +1,13 @@
+{
+ "name":"axon-addressbook-nodejs",
+ "description":"An application to show addresses using an axonframework backend",
+ "version":"0.1.0",
+ "author":"Jettro Coenradie <jettro@gridshore.nl>",
+ "dependencies":{
+ "express":"",
+ "jade":"",
+ "":""
+ },
+ "main":"app.js"
+
+}

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,12 @@
+/**
+ * Test class for ContactRepository
+ *
+ * @author Jettro Coenradie
+ */
+var assert = require('assert');
+var ContactRepository = require("../ContactRepository");
+var repository = ContactRepository.createRepo('localhost',8080);
+
+repository.listContacts(function(contacts){
+ assert.equal(2,contacts.length, "Number of contacts is not right");
+});
View
@@ -0,0 +1,9 @@
+h2 Contacts
+
+table
+ - each contact in contacts
+ tr
+ td #{contact.identifier}
+ td #{contact.name}
+
+
View
@@ -0,0 +1,14 @@
+!!! 5
+html(lang="en")
+ head
+ title Nodejs presenter
+ link(href="/style/main.css", media="screen", rel="stylesheet", type="text/css")
+ script(src='/js/jquery-1.6.1.min.js')
+ script(src='/js/local.js')
+ body
+ div#heading
+ p header
+ div#body!= body
+ div#footer
+ p footer
+

0 comments on commit 0c312e2

Please sign in to comment.