Skip to content

Commit c0b957d

Browse files
committed
Specify port via ENV
1 parent b3ebd2b commit c0b957d

2 files changed

Lines changed: 38 additions & 21 deletions

File tree

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Serve static files or import as module in your project.
1212
[![npm version](https://img.shields.io/npm/v/https-localhost.svg)](https://www.npmjs.com/package/https-localhost?activeTab=versions)
1313

1414

15-
### Install and trust the certificate
15+
## Install and trust the certificate
1616
Add the root certificate we just generated to your list of trusted certificates.
1717
This step depends on the operating system you're running:
1818

@@ -23,15 +23,15 @@ menu and select the file. Then double-click on the certificate and select always
2323
the generated root certificate as trusted.
2424

2525

26-
### Run
26+
## Run
2727

28-
#### Use standalone
28+
### Use standalone
2929
From terminal navigate into the folder and run `sudo npm install -g` to install this tool globally.
3030

3131
Then serve static file with `sudo serve <static-path>`.
3232

3333

34-
#### Use as module
34+
### Use as module
3535
Install the dependency with `npm install -s https-localhost`.
3636
Then just require this module, it will start the server automatically.
3737

@@ -41,6 +41,14 @@ const app = require('https-localhost')
4141
app.get('/', (req, res) => res.send('Hello World!'))
4242
```
4343

44+
#### Specify the port
45+
You can specify the port number of the SSL port with `sudo PORT=<port-number> serve <static-path>`
46+
or from another module with `process.env.PORT = <port-number>`.
47+
48+
If you specify a port number the http redirect to https will be disabled.
49+
You can activate the http redirect again specifying not only the PORT environment variable but also the HTTP_PORT one.
50+
51+
#### Close the server
4452
You can close the server with `app.close()`.
4553
Closing the server is an async operation, you can get notified with `app.close(callback)` or `app.close().then(...)`.
4654

index.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ const https = require('spdy') // using HTTP/2: spdy will be deprecated soon, wai
77
const express = require('express')
88
const compression = require('compression')
99

10-
// SSL certificate
10+
// SSL CERTIFICATE
1111
const certOptions = {
1212
key: fs.readFileSync(path.resolve(__dirname + "/cert/server.key")),
1313
cert: fs.readFileSync(path.resolve(__dirname + "/cert/server.crt"))
1414
}
1515

16-
// run express on 443
16+
const port = process.env.PORT || 443
17+
18+
19+
// START THE APP
20+
21+
// run express
1722
const app = express()
18-
app.server = https.createServer(certOptions, app).listen(443)
23+
app.server = https.createServer(certOptions, app).listen(port)
24+
1925
// save sockets for fast close
2026
const sockets = []
2127
let nextSocketId = 0
@@ -30,27 +36,30 @@ app.use(compression())
3036
app.set('json spaces', 0)
3137

3238
// redirect http to https
33-
app.http = http.createServer(function (req, res) {
34-
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url })
35-
res.end()
36-
}).listen(80)
37-
// save sockets for fast close
38-
app.server.on('connection', socket => {
39-
const socketId = nextSocketId++
40-
sockets[socketId] = socket
41-
socket.on('close', () => delete sockets[socketId])
42-
})
39+
if (port === 443 || process.env.HTTP_PORT) {
40+
app.http = http.createServer(function (req, res) {
41+
res.writeHead(301, {"Location": "https://" + req.headers['host'] + req.url})
42+
res.end()
43+
}).listen(process.env.HTTP_PORT || 80)
4344

44-
// ready
45-
if (!process.env.TEST) console.info("Server running on port 443.")
45+
app.http.on('connection', socket => {
46+
const socketId = nextSocketId++
47+
sockets[socketId] = socket
48+
socket.on('close', () => delete sockets[socketId])
49+
})
50+
}
4651

47-
// serve static files, launch as: 'node index.js <static-path>'
52+
// SERVE STATIC FILES, if launched as: 'node index.js <static-path>'
4853
if (require.main === module) { // called directly (not through require)
4954
const staticPath = process.argv[2]
5055
app.use(express.static(staticPath || process.cwd()))
5156
}
5257

53-
// close the app
58+
// ready
59+
if (!process.env.TEST) console.info("Server running on port " + port + ".")
60+
61+
62+
// CLOSE THE APP
5463
app.close = (callback) => {
5564
const promises = [
5665
new Promise(resolve => app.http.close(resolve)),

0 commit comments

Comments
 (0)