Permalink
Browse files

Allow for setting options and explicitly calling the request handler

  • Loading branch information...
1 parent 56e12d8 commit 74db6a0d7d54df1fc6b2283d4dad0fe785b1710a @marcuswestin committed Aug 10, 2011
Showing with 10 additions and 8 deletions.
  1. +10 −8 server.js
View
@@ -10,7 +10,9 @@ module.exports = {
isRequireRequest: isRequireRequest,
addPath: addPath,
addFile: addFile,
- addReplacement: addReplacement
+ addReplacement: addReplacement,
+ setOpts: setOpts,
+ handleRequest: handleRequest
}
function addReplacement(searchFor, replaceWith) {
@@ -30,28 +32,28 @@ function addFile() {
function listen(port, _opts) {
if (!_opts) { _opts = { port:port }}
- _setOpts(_opts)
+ setOpts(_opts)
opts.handleAllRequests = true
var server = http.createServer()
mount(server)
server.listen(opts.port, opts.host)
}
function mount(server, _opts, handleAllRequests) {
- _setOpts(_opts)
+ setOpts(_opts)
server.on('request', function(req, res) {
if (isRequireRequest(req) || opts.handleAllRequests) {
- _handleRequest(req, res)
+ handleRequest(req, res)
}
})
return server
}
function connect(opts) {
- _setOpts(opts)
+ setOpts(opts)
return function require(req, res, next) {
if (!isRequireRequest(req)) { return next() }
- _handleRequest(req, res)
+ handleRequest(req, res)
}
}
@@ -68,7 +70,7 @@ var opts = {
host: 'localhost'
}
-function _setOpts(_opts) {
+function setOpts(_opts) {
opts = util.extend(_opts, opts)
}
@@ -78,7 +80,7 @@ function _normalizeURL(url) {
/* request handlers
******************/
-function _handleRequest(req, res) {
+function handleRequest(req, res) {
var reqPath = _normalizeURL(req.url).substr(opts.root.length + 2)
if (!reqPath.match(/\.js$/)) {
_handleMainModuleRequest(reqPath, req, res)

0 comments on commit 74db6a0

Please sign in to comment.