live-server
live-server copied to clipboard
https config cannot be asynchronous
The https configuration requires a synchronous export from a module. But in some cases it would be useful to export a promise:
Using fs
asynchronous functions
With fs
callback functions (kind of ugly):
const fs = require('fs')
module.exports = Promise.all([
new Promise((resolve, reject) => {
fs.readFile(__dirname + '/server.cert', (error, cert) => error ? reject(error) : resolve(cert))
}),
new Promise((resolve, reject) => {
fs.readFile(__dirname + '/server.key', (error, key) => error ? reject(error) : resolve(key))
})
]).then(([cert, key]) => ({ cert, key, passphrase: '12345' }))
With fs
experimental promises or fs-extra
:
const fs = require('fs').promises // const fs = require('fs-extra')
module.exports = Promise.all([
fs.readFile(__dirname + '/server.cert'),
fs.readFile(__dirname + '/server.key')
]).then(([cert, key]) => ({ cert, key, passphrase: '12345' }))
Using devcert
:
const devcert = require('devcert')
module.exports = (async () => ({
...(await devcert.certificateFor('localhost')),
passphrase: '12345'
})()
Can be one-lined when passphrase is not needed:
module.exports = require('devcert').certificateFor('localhost')
Personally, I would prefer being able to pass an object to the https
option, so asynchronous stuff can be handled by the user, outside live-server:
const liveServer = require('live-server')
const devcert = require('devcert')
;(async () => liveServer.start({
// ...
https: await devcert.certificateFor('localhost')
})()