Permalink
Browse files

More refactoring server.

  • Loading branch information...
1 parent 3f97798 commit 58efcb8783fe0e714ecd3facf609f00eee72a353 @airportyh airportyh committed Mar 16, 2013
Showing with 45 additions and 48 deletions.
  1. +45 −48 lib/server.js
View
@@ -29,76 +29,72 @@ require('./socket.io.patch')
function Server(app){
this.app = app
-
- // Build the server
- this.exp = Express()
- this.initServer()
+ this.config = this.app.config
+ this.createExpress()
this.ieCompatMode = null
}
Server.prototype = {
__proto__: EventEmitter.prototype
, start: function(){
// Start the server!
// Create socket.io sockets
- this.server = http.createServer(this.exp)
+ this.server = http.createServer(this.express)
this.io = SocketIO.listen(this.server)
this.io.sockets.on('connection', this.onClientConnected.bind(this))
- this.server.listen(this.app.config.get('port'))
+ this.server.listen(this.config.get('port'))
process.nextTick(function(){
this.emit('server-start')
}.bind(this))
}
, stop: function(callback){
this.server.close(callback)
}
- , configure: function(){
+ , createExpress: function(){
var self = this
- var exp = this.exp
- exp.configure(function(){
- exp.engine("html", Mustache)
- exp.set("view options", {layout: false})
- exp.use(function(req, res, next){
- if (self.ieCompatMode)
- res.setHeader('X-UA-Compatible', 'IE=' + self.ieCompatMode)
- next()
- })
- exp.use(Express.static(__dirname + '/../public'))
- })
- }
- , initServer: function(){
- var self = this
- var config = this.app.config
- var exp = this.exp
-
- this.configure()
+ var app = this.express = Express()
+
+ this.configureExpress(app)
- exp.get('/', function(req, res){
+ app.get('/', function(req, res){
self.serveHomePage(req, res)
})
- exp.get('/testem.js', function(req, res){
+ app.get('/testem.js', function(req, res){
self.serveTestemClientJs(req, res)
})
// Everything falls back to serving a static file from the FS
- exp.get(/^(.+)$/, function(req, res){
+ app.get(/^(.+)$/, function(req, res){
self.serveStaticFile(req.params[0], req, res)
})
-
+ }
+ , configureExpress: function(app){
+ var self = this
+
+ app.configure(function(){
+ app.engine("html", Mustache)
+ app.set("view options", {layout: false})
+ app.use(function(req, res, next){
+ if (self.ieCompatMode)
+ res.setHeader('X-UA-Compatible', 'IE=' + self.ieCompatMode)
+ next()
+ })
+ app.use(Express.static(__dirname + '/../public'))
+ })
}
, renderRunnerPage: function(err, files, res){
- var config = this.app.config
+ var config = this.config
var framework = config.get('framework') || 'jasmine'
var css_files = config.get('css_files')
- var runnerPage = {
- jasmine: __dirname + '/../views/jasminerunner.html'
- , qunit: __dirname + '/../views/qunitrunner.html'
- , mocha: __dirname + '/../views/mocharunner.html'
- , 'mocha+chai': __dirname + '/../views/mochachairunner.html'
- , buster: __dirname + '/../views/busterrunner.html'
- , custom: __dirname + '/../views/customrunner.html'
+ var templateFile = {
+ jasmine: 'jasminerunner.html'
+ , qunit: 'qunitrunner.html'
+ , mocha: 'mocharunner.html'
+ , 'mocha+chai': 'mochachairunner.html'
+ , buster: 'busterrunner.html'
+ , custom: 'customrunner.html'
}[framework]
- res.render(runnerPage, {
+ res.render(__dirname + '/../views/' + templateFile, {
scripts: files,
styles: css_files
})
@@ -109,7 +105,7 @@ Server.prototype = {
var self = this
- var config = this.app.config
+ var config = this.config
var test_page = config.get('test_page')
if (test_page){
@@ -122,7 +118,7 @@ Server.prototype = {
}
}
, serveHomePage: function(req, res){
- var config = this.app.config
+ var config = this.config
var routes = config.get('routes') || config.get('route') || {}
if (routes['/']){
this.serveStaticFile('/', req, res)
@@ -135,14 +131,15 @@ Server.prototype = {
res.write(';(function(){')
var files = [
- __dirname + '/../public/testem/socket.io.js'
- , __dirname + '/../public/testem/jasmine_adapter.js'
- , __dirname + '/../public/testem/qunit_adapter.js'
- , __dirname + '/../public/testem/mocha_adapter.js'
- , __dirname + '/../public/testem/buster_adapter.js'
- , __dirname + '/../public/testem/testem_client.js'
+ 'socket.io.js'
+ , 'jasmine_adapter.js'
+ , 'qunit_adapter.js'
+ , 'mocha_adapter.js'
+ , 'buster_adapter.js'
+ , 'testem_client.js'
]
async.forEachSeries(files, function(file, done){
+ file = __dirname + '/../public/testem/' + file
fs.readFile(file, function(err, data){
if (err){
res.write('// Error reading ' + file + ': ' + err)
@@ -165,7 +162,7 @@ Server.prototype = {
delete req.headers['if-none-match']
}
, route: function route(uri){
- var config = this.app.config
+ var config = this.config
var routes = config.get('routes') || config.get('route') || {}
var bestMatchLength = 0
var bestMatch = null
@@ -184,7 +181,7 @@ Server.prototype = {
}
, serveStaticFile: function(uri, req, res){
var self = this
- var config = this.app.config
+ var config = this.config
var routeRes = this.route(uri)
uri = routeRes.uri
var wasRouted = routeRes.routed

0 comments on commit 58efcb8

Please sign in to comment.