Skip to content

Commit d36166f

Browse files
committed
Added directory option
1 parent f7bdaa6 commit d36166f

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
-p, --port <PORT_NUMBER> (8080)
1111
-c, --cors <CORS_STRING>
1212
13+
-d, --dir <DIRECTORY> (.)
14+
1315
-lr, -lR, --logRequest [<BOOLEAN>|<NUMBER>] (true)
1416
-lrh, -lRH, --logRequestHeaders [<BOOLEAN>|<NUMBER>] (false)
1517
-lra, -lRA, --logRequestAddress [<BOOLEAN>|<NUMBER>] (true)

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ function processArgs () {
2323
options.cors = arg.substring(7)
2424
} else if (arg.indexOf('-c=') === 0) {
2525
options.cors = arg.substring(3)
26+
} else if (arg.indexOf('--dir=') === 0) {
27+
options.directory = arg.substring(6)
28+
} else if (arg.indexOf('-d=') === 0) {
29+
options.directory = arg.substring(3)
2630
} else if (arg.indexOf('--logRequest') === 0) {
2731
if (arg === '--logRequest' ||
2832
arg === '--logRequest=true' ||

lib/server.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
var os = require('os')
34
var http = require('http')
45
var url = require('url')
56
var fs = require('fs')
@@ -14,6 +15,7 @@ var types = require('./file-content-types')
1415
function Server (options) {
1516
this._port = 8080
1617
this._cors = null
18+
this._directory = '.'
1719
this._indexUrl = url.parse('/index.html')
1820
this._rootLookForIndex = true
1921
this._logRequest = true
@@ -34,6 +36,10 @@ function Server (options) {
3436
this._cors = options.cors
3537
}
3638

39+
if (typeof options.directory === 'string' && options.directory) {
40+
this._directory = options.directory
41+
}
42+
3743
if (typeof options.logRequest === 'boolean') {
3844
this._logRequest = options.logRequest
3945
}
@@ -47,6 +53,8 @@ function Server (options) {
4753
}
4854
}
4955

56+
this._directory = Server._resolvePath(this._directory)
57+
5058
this._server = http.createServer(
5159
this._handler.bind(this)
5260
)
@@ -87,6 +95,22 @@ Server._CONTENT_TYPE = 'Content-Type'
8795
*/
8896
Server._ACCESS_CONTROL_ALLOW_ORIGIN = 'Access-Control-Allow-Origin'
8997

98+
/**
99+
* @private
100+
* @param {string} path
101+
* @returns {string}
102+
*/
103+
Server._resolvePath = function (path) {
104+
var char
105+
if (path.charAt(0) === '~') {
106+
char = path.charAt(1)
107+
if (char === '$' || char === '/' || char === '\\') {
108+
return os.homedir() + path.substring(1)
109+
}
110+
}
111+
return path
112+
}
113+
90114
/**
91115
* @private
92116
* @param {object} request
@@ -147,7 +171,7 @@ Server.prototype._onError = function (error) {
147171
* @returns {string}
148172
*/
149173
Server.prototype._getPath = function (urlPath) {
150-
return path.join('.', urlPath)
174+
return path.join(this._directory, urlPath)
151175
}
152176

153177
/**

0 commit comments

Comments
 (0)