UNPKG

940 Btext/coffeescriptView Raw
1{join} = require 'path'
2fs = require 'fs'
3
4http = require 'http'
5https = require 'https'
6
7read = (file) -> fs.readFileSync file, 'utf8'
8
9config =
10 port: 4000
11 ssl: false
12
13module.exports = (port, app, done) ->
14 app ?= ->
15 done ?= ->
16
17 if config.ssl
18
19 # read cert files
20 ca = config.ssl.ca || []
21 options =
22 key: read config.ssl.key
23 cert: read config.ssl.cert
24 ca: ca.map read
25
26 # create server with ssl
27 server = https.createServer(options, app).listen port, done
28
29 #http server to redirect to https
30 if (port is config.port) and config.ssl.redirectFrom?
31 redirect = (req, res) ->
32 redirectTarget = "https://#{req.headers.host}#{req.url}"
33 res.writeHead 301, {
34 "Location": redirectTarget
35 }
36 res.end()
37 redirectServer = http.createServer(redirect).listen config.ssl.redirectFrom
38
39 return server
40
41 else
42 http.createServer(app).listen port, done