Refactor to remove switch statement

parent 03c1e0e5
......@@ -7,33 +7,24 @@ const options = {
cert: fs.readFileSync('localhost.pem')
}
const routes = {
'/': 'index.html',
'/keys.js': 'keys.js',
'/sodium.js': 'sodium.js',
'/favicon.ico': 'favicon.ico'
}
https.createServer(options, (request, response) => {
const url = request.url
console.log(`URL: ${url}`)
switch (url) {
case '/':
pipeline(fs.createReadStream('index.html'), response)
break;
case '/keys.js':
pipeline(fs.createReadStream('keys.js'), response)
break;
case '/sodium.js':
pipeline(fs.createReadStream('sodium.js'), response)
break;
case '/favicon.ico':
pipeline(fs.createReadStream('favicon.ico'), response)
break;
default:
console.log(`Unknown path requested: ${url}`)
response.statusCode = 404
response.end('Not found.')
break;
const fileToServe = routes[url]
if (fileToServe !== undefined) {
response.statusCode = 200
pipeline(fs.createReadStream(fileToServe), response)
} else {
console.log(`Unknown path requested: ${url}`)
response.statusCode = 404
response.end('Not found.')
}
}).listen(443, () => {
console.log('Listening on port 443.')
......
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