Verified Commit ae4a15e8 authored by Aral Balkan's avatar Aral Balkan
Browse files

Actually serve the requested directory & add manual port selection

parent 52b5b3f1
......@@ -7,18 +7,24 @@ const os = require('os')
const arguments = process.argv
if (arguments.length > 3) {
console.log('Usage: https-server [folder-to-serve]')
if (arguments.length > 4) {
console.log('Usage: https-server [folder-to-serve (default=.)] [port (default=443)]')
process.exit()
}
// If no path is passed, serve the current folder.
// If there is a path, serve that.
let pathToServe = '.'
if (arguments.length === 3) {
if (arguments.length >= 3) {
pathToServe = arguments[2]
}
let port = 443
// If a port is given, use that.
if (arguments.length === 4) {
port = parseInt(arguments[3])
}
if (!fs.existsSync(pathToServe)) {
console.log(`\n 🤔 Error: could not find path ${pathToServe}\n`)
process.exit(1)
......@@ -41,14 +47,19 @@ if (!fs.existsSync(nodecertDirectory)) {
const app = express()
app.use(morgan('tiny'))
app.use(express.static('dist'))
app.use(express.static(pathToServe))
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`)
}, app).listen(port, () => {
const serverPort = server.address().port
let portSuffix = ''
if (serverPort !== 443) {
portSuffix = `:${serverPort}`
}
console.log(`\n 🎉 Serving ${pathToServe} on https://localhost${portSuffix}\n`)
})
} catch (error) {
console.log('\nError: could not start server', error)
......
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