Commit 9a64f188 authored by Aral Balkan's avatar Aral Balkan

Initial add

parents
node_modules
# https-server
__Work in progress__ Please do not use yet.
An HTTPS server that uses [nodecert](https://source.ind.ie/hypha/tools/nodecert).
## Design goals
* __Command-line use:__ https-server _directory_
* Easy integration into Express
## Installation
```sh
npm i -g @ind.ie/https-server
```
(On macOS, you must [manually install the dependency](#macos-dependency) for now.)
## Usage
### Commandline
```sh
nodecert <folder-to-serve>
```
If you do not already have TLS certificates, they will be created for you automatically using nodecert.
#!/usr/bin/env node
require('../index.js')
const https = require('https')
const fs = require('fs')
const express = require('express')
const morgan = require('morgan')
const path = require('path')
const os = require('os')
const arguments = process.argv
if (arguments.length > 3) {
console.log('Usage: https-server [folder-to-serve]')
process.exit()
}
// If no path is passed, serve the current folder.
// If there is a path, serve that.
let pathToServe = '.'
if (arguments.length === 3) {
pathToServe = arguments[2]
}
if (!fs.existsSync(pathToServe)) {
console.log(`\n 🤔 Error: could not find path ${pathToServe}\n`)
process.exit(1)
}
// TODO: Require and run node-cert to ensure that certificates exist.
const nodecertDirectory = path.join(os.homedir(), '.nodecert')
if (!fs.existsSync(nodecertDirectory)) {
console.log('\nError: requires nodecert.\n\nInstall: npm i -g nodecert\nRun : nodecert\n\nMore information: https://source.ind.ie/hypha/tools/nodecert\n')
process.exit(1)
}
const app = express()
app.use(morgan('tiny'))
app.use(express.static('dist'))
try {
const server = https.createServer({
key: fs.readFileSync(path.join(nodecertDirectory, 'localhost-key.pem')),
cert: fs.readFileSync(path.join(nodecertDirectory, 'localhost.pem'))
}, app).listen(443, () => {
console.log(`\n 🎉 Serving ${pathToServe} on https://localhost\n`)
})
} catch (error) {
console.log('\nError: could not start server', error)
process.exit(1)
}
This diff is collapsed.
{
"name": "@ind.ie/https-server",
"version": "1.0.0",
"description": "HTTPS server that uses nodecert",
"main": "index.js",
"bin": "bin/https-server.js",
"scripts": {
"start": "node index.js"
},
"repository": {
"type": "git",
"url": "git@source.ind.ie:hypha/tools/https-server.git"
},
"author": {
"name": "Aral Balkan",
"email": "aral@ind.ie",
"url": "https://ar.al"
},
"license": "AGPL-3.0-or-later",
"dependencies": {
"express": "^4.16.4",
"morgan": "^1.9.1"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment