|
5 | 5 | * Released under the MIT License |
6 | 6 | */ |
7 | 7 |
|
8 | | -var http = require('http'), |
| 8 | +var https = require('https'), |
| 9 | + http = require('http'), |
9 | 10 | path = require('path'), |
10 | 11 | url = require('url'), |
| 12 | + fs = require('fs'), |
11 | 13 | mapping = require('./mapping'); |
12 | 14 |
|
13 | 15 | var help = {}; |
@@ -142,31 +144,40 @@ function route(req, res) { |
142 | 144 | } |
143 | 145 |
|
144 | 146 | module.exports = { |
145 | | - listen: function listen(local, callback) { |
146 | | - var httpServer = http.createServer(route.bind(this)); |
| 147 | + listen: function listen(local, options, callback) { |
| 148 | + var server; |
| 149 | + |
| 150 | + if (fs.existsSync(options.key) && fs.existsSync(options.cert)) { |
| 151 | + server = https.createServer({ |
| 152 | + key: fs.readFileSync(path.resolve(process.cwd(), options.key)), |
| 153 | + cert: fs.readFileSync(path.resolve(process.cwd(), options.cert)) |
| 154 | + }, route.bind(this)); |
| 155 | + } else { |
| 156 | + server = http.createServer(route.bind(this)); |
| 157 | + } |
147 | 158 |
|
148 | | - httpServer.on('error', function listenError(err) { |
149 | | - if (typeof callback === 'function') { |
150 | | - callback(err); |
151 | | - } |
152 | | - }).on('listening', function listenError() { |
153 | | - if (typeof callback === 'function') { |
154 | | - callback(undefined, { |
155 | | - server: httpServer, |
156 | | - protocol: 'http', |
| 159 | + server.on('error', function listenError(err) { |
| 160 | + if (typeof callback === 'function') { |
| 161 | + callback(err); |
| 162 | + } |
| 163 | + }).on('listening', function listenError() { |
| 164 | + if (typeof callback === 'function') { |
| 165 | + callback(undefined, { |
| 166 | + server: server, |
| 167 | + protocol: server instanceof https.Server ? 'https' : 'http', |
| 168 | + hostname: local.hostname, |
| 169 | + port: local.port, |
| 170 | + url: url.format({ |
| 171 | + protocol: server instanceof https.Server ? 'https' : 'http', |
157 | 172 | hostname: local.hostname, |
158 | | - port: local.port, |
159 | | - url: url.format({ |
160 | | - protocol: 'http', |
161 | | - hostname: local.hostname, |
162 | | - port: local.port |
163 | | - }) |
164 | | - }); |
165 | | - } |
166 | | - }).listen(local.port); |
| 173 | + port: local.port |
| 174 | + }) |
| 175 | + }); |
| 176 | + } |
| 177 | + }).listen(local.port); |
167 | 178 |
|
168 | 179 | buildHelp(url.format(this.config.hostname)); |
169 | 180 |
|
170 | | - return httpServer; |
| 181 | + return server; |
171 | 182 | } |
172 | 183 | }; |
0 commit comments