Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fojas committed May 21, 2012
0 parents commit c806d1a
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
31 changes: 31 additions & 0 deletions app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

io = require('socket.io')
express = require('express')
routes = require('./routes')
sockets = require('./sockets')

app = module.exports = express.createServer()


app.configure () ->
app.set('views', __dirname + '/views')
app.set('view engine', 'hbs')
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(app.router)
app.use(express.static(__dirname + '/public'))

app.configure 'development', () ->
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))


app.configure 'production', () ->
app.use(express.errorHandler())

app.get('/', routes.index)

app.listen 3000, () ->
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env)

sio = io.listen(app)
sio.of('/'+command).on('connection', sockets[command].connection) for command in sockets
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "base-app"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.5.8"
, "coffee-script": "1.3.3"
, "hbs": "1.0.2"
, "socket.io": "0.9.6"
, "supervisor": "0.3.1"
}
, "scripts" : {
"start": "./node_modules/.bin/supervisor -e \"node|js|coffee\" run.js"
, "test": "./node_modules/.bin/mocha $(find test -name \"*_test.coffee\")"
}
, "devDependencies": {
"should": "0.6.3"
, "mocha": "1.0.3"
, "sinon": "1.3.4"
}
}
8 changes: 8 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
2 changes: 2 additions & 0 deletions routes/index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.index = (req, res) ->
res.render('index', { title: 'Express' })
2 changes: 2 additions & 0 deletions run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('coffee-script');
require('./app');
2 changes: 2 additions & 0 deletions sockets.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require("fs").readdirSync("./sockets").forEach (file) ->
module.exports[file.split('.')[0]] = require("./sockets/" + file)
4 changes: 4 additions & 0 deletions sockets/index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
connection : (socket) ->
console.log "index socket connected"
}
2 changes: 2 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--compilers coffee:coffee-script
--reporter dot
12 changes: 12 additions & 0 deletions test/routes/index_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
routes = require ('../../routes')
should = require('should')

describe 'Routes', ->
describe 'Index', ->
it 'should respond to default route', (done)->
routes.index {}, {
render: (viewName,options) ->
viewName.should.equal("index")
options.title.should.equal("Express")
done()
}
10 changes: 10 additions & 0 deletions test/sockets/index_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sockets = require '../../sockets'
should = require('should')

describe "Socket", ->
describe "Index", ->
it "should have an index channel", ->
should.exist sockets.index

it "should have a connection callback for index", ->
should.exist sockets.index.connection
106 changes: 106 additions & 0 deletions test/support/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

/**
* visionmedia <3
* Module dependencies.
*/

var EventEmitter = require('events').EventEmitter
, methods = require('express').router.methods
, http = require('http');

module.exports = request;

function request(app) {
return new Request(app);
}

function Request(app) {
var self = this;
this.data = [];
this.header = {};
this.app = app;
if (!this.server) {
this.server = http.Server(app);
this.server.listen(0, function(){
self.addr = self.server.address();
self.listening = true;
});
}
}

/**
* Inherit from ventEmitter.prototype
*/

Request.prototype.__proto__ = EventEmitter.prototype;

methods.forEach(function(method){
Request.prototype[method] = function(path){
return this.request(method, path);
};
});

Request.prototype.set = function(field, val){
this.header[field] = val;
return this;
};

Request.prototype.write = function(data){
this.data.push(data);
return this;
};

Request.prototype.request = function(method, path){
this.method = method;
this.path = path;
return this;
};

Request.prototype.expect = function(body, fn){
this.end(function(res){
if ('number' == typeof body) {
res.statusCode.should.equal(body);
} else if (body instanceof RegExp) {
res.body.should.match(body);
} else {
res.body.should.equal(body);
}
fn();
});
};

Request.prototype.end = function(fn){
var self = this;

if (this.listening) {
var req = http.request({
method: this.method
, port: this.addr.port
, host: this.addr.address
, path: this.path
, headers: this.header
});

this.data.forEach(function(chunk){
req.write(chunk);
});

req.on('response', function(res){
var buf = '';
res.setEncoding('utf8');
res.on('data', function(chunk){ buf += chunk });
res.on('end', function(){
res.body = buf;
fn(res);
});
});

req.end();
} else {
this.server.on('listening', function(){
self.end(fn);
});
}

return this;
};
2 changes: 2 additions & 0 deletions views/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>{{title}}</h1>
<p>Welcome to {{title}}</p>
10 changes: 10 additions & 0 deletions views/layout.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
<link rel="stylesheet" href="/stylesheets/style.css" />
</head>
<body>
{{{body}}}
</body>
</html>

0 comments on commit c806d1a

Please sign in to comment.