Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simplify require server options

  • Loading branch information...
commit 8578368f4c728f887a7fd00ed6185b990c9d8a15 1 parent 3d0ba00
@marcuswestin authored
Showing with 23 additions and 29 deletions.
  1. +2 −2 README.md
  2. +1 −1  package.json
  3. +20 −26 server.js
View
4 README.md
@@ -49,7 +49,7 @@ Use programmatically
====================
In node:
- require('require/server').listen(1234, __dirname + '/example')
+ require('require/server').listen(1234)
or mount it on an http server you're already running
@@ -61,7 +61,7 @@ or, as connect middleware
connect.createServer(
connect.static(__dirname + '/example'),
- require('require/server').connect(__dirname + '/example')
+ require('require/server').connect()
)
Compile programmatically:
View
2  package.json
@@ -16,7 +16,7 @@
"author": "Marcus Westin <narcvs@gmail.com> (http://marcuswest.in)",
"dependencies": {
"uglify-js": "1.2.6",
- "std": "0.1.39"
+ "std": "0.1.40"
},
"devDependencies": {},
"directories": {}
View
46 server.js
@@ -2,6 +2,7 @@ var http = require('http')
var fs = require('fs')
var path = require('path')
var extend = require('std/extend')
+var isObject = require('std/isObject')
var getDependencyList = require('./lib/getDependencyList')
var getRequireStatements = require('./lib/getRequireStatements')
var getCode = require('./lib/getCode')
@@ -12,35 +13,30 @@ module.exports = {
mount: mount,
connect: connect,
isRequireRequest: isRequireRequest,
- setOpts: setOpts,
handleRequest: handleRequest
}
-function listen(port, _opts) {
- if (!_opts) { _opts = { port:port }}
- else if(port) { _opts.port= port }
- setOpts(_opts)
+function listen(portOrOpts) {
+ var _opts = (isObject(portOrOpts) ? portOrOpts : { port:portOrOpts || 1234 })
opts.handleAllRequests = true
- var server = http.createServer()
- mount(server)
- server.listen(opts.port, opts.host)
+ mount(http.createServer(), _opts).listen(opts.port, opts.host)
}
-function mount(server, _opts, handleAllRequests) {
+function mount(server, _opts) {
setOpts(_opts)
- server.on('request', function(req, res) {
- if (isRequireRequest(req) || opts.handleAllRequests) {
- handleRequest(req, res)
- }
- })
- return server
+ return server.on('request', _checkRequest)
}
function connect(opts) {
setOpts(opts)
- return function require(req, res, next) {
- if (!isRequireRequest(req)) { return next() }
+ return _checkRequest
+}
+
+function _checkRequest(req, res, next) {
+ if (isRequireRequest(req) || opts.handleAllRequests) {
handleRequest(req, res)
+ } else {
+ next && next()
}
}
@@ -61,18 +57,18 @@ function setOpts(_opts) {
opts = extend(_opts, opts)
}
-function _normalizeURL(url) {
- return url.replace(/\?.*/g, '').replace(/\/js$/, '.js')
-}
-
/* request handlers
******************/
function handleRequest(req, res) {
var reqPath = _normalizeURL(req.url).substr(opts.root.length + 2)
- if (!reqPath.match(/\.js$/)) {
- _handleMainModuleRequest(reqPath, req, res)
- } else {
+ if (reqPath.match(/\.js$/)) {
_handleModuleRequest(reqPath, res)
+ } else {
+ _handleMainModuleRequest(reqPath, req, res)
+ }
+
+ function _normalizeURL(url) {
+ return url.replace(/\?.*/g, '').replace(/\/js$/, '.js')
}
}
@@ -80,8 +76,6 @@ function _handleMainModuleRequest(reqPath, req, res) {
var modulePath = resolve.path('./' + reqPath, opts.path)
if (!modulePath) { return _sendError(res, 'Could not find module "'+reqPath+'" from "'+opts.path+'"') }
-
-
try { var deps = getDependencyList(modulePath) }
catch(err) { return _sendError(res, 'in getDependencyList: ' + err) }
Please sign in to comment.
Something went wrong with that request. Please try again.