Skip to content

Commit 7c4ef51

Browse files
committed
Running as standalone or as module
1 parent 89209b2 commit 7c4ef51

4 files changed

Lines changed: 69 additions & 35 deletions

File tree

README.md

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
# HTTPS server running on localhost
2+
Run an express server on localhost with HTTP2 and SSL for free.
3+
4+
Serve static files or import as module in your project
5+
26

37
### Install and trust the certificate
48
Add the root certificate we just generated to your list of trusted certificates.
59
This step depends on the operating system you're running:
10+
611
- Mac OS: open Keychain Access, choose System from the left navigation bar, choose "Import items..." from the File app
712
menu and select the file. Then double-click on the certificate and select always-trust in the Trust panel.
13+
814
- Linux: Depending on your Linux distribution, you can use `trust`, `update-ca-certificates` or another command to mark
915
the generated root certificate as trusted.
1016

17+
1118
### Run
12-
1. Edit [server.js](server.js):
13-
- serve static files with `app.use(express.static('<path-to-serve>'))`
14-
- use express as backend, i.e.: `app.get('/', (req, res) => res.send('Hello World!'))`
15-
2. Install dependency with `npm install`
16-
3. Run with `sudo npm start` or `sudo node serve.js` (root required to use port 443)
1719

18-
### License
19-
[AGPL-3.0](LICENSE)
20+
#### Use standalone
21+
From terminal navigate into the folder and run `sudo npm install -g` to install this tool globally.
2022

21-
**Thanks to:** [Daksh Shah](https://github.com/dakshshah96)
23+
Then serve static file with `sudo serve <static-path>`.
24+
25+
26+
#### Use as module
27+
Just require this module. It will start the server automatically.
28+
29+
For example, put in your index.js file:
30+
```
31+
const app = require('./index.js')
32+
app.get('/', (req, res) => res.send('Hello World!'))
33+
```
34+
35+
---
36+
37+
### License
38+
[AGPL-3.0](LICENSE)

index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env node
2+
3+
const path = require('path')
4+
const fs = require('fs')
5+
const http = require('http')
6+
const https = require('spdy') // using HTTP/2: spdy will be deprecated soon, waiting for HTTP/2 on https module.
7+
const express = require('express')
8+
const compression = require('compression')
9+
10+
// SSL certificate
11+
const certOptions = {
12+
key: fs.readFileSync(path.resolve(__dirname + "/cert/server.key")),
13+
cert: fs.readFileSync(path.resolve(__dirname + "/cert/server.crt"))
14+
}
15+
16+
// run express on 443
17+
const app = express()
18+
https.createServer(certOptions, app).listen(443)
19+
20+
// gzip compression and minify
21+
app.use(compression())
22+
app.set('json spaces', 0)
23+
24+
// redirect http to https
25+
http.createServer(function (req, res) {
26+
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url })
27+
res.end()
28+
}).listen(80)
29+
30+
// ready
31+
console.info("Server running on port 443.")
32+
33+
// serve static files, launch as: 'node index.js <static-path>'
34+
const staticPath = process.argv[2]
35+
if (staticPath) app.use(express.static(staticPath))
36+
37+
// export as module
38+
module.exports = app

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "https-localhost",
3-
"version": "1.0.0",
3+
"version": "0.2.0",
44
"description": "HTTPS server running on localhost",
5-
"main": "server.js",
5+
"main": "index.js",
66
"scripts": {
7-
"start": "node server.js",
8-
"http2": "HTTP2=true node server.js"
7+
"start": "node index.js"
8+
},
9+
"bin": {
10+
"serve": "./index.js"
911
},
1012
"repository": {
1113
"type": "git",

server.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)