11'use strict'
22
3+ var os = require ( 'os' )
34var http = require ( 'http' )
45var url = require ( 'url' )
56var fs = require ( 'fs' )
@@ -14,6 +15,7 @@ var types = require('./file-content-types')
1415function 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 */
8896Server . _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 */
149173Server . prototype . _getPath = function ( urlPath ) {
150- return path . join ( '.' , urlPath )
174+ return path . join ( this . _directory , urlPath )
151175}
152176
153177/**
0 commit comments