Skip to content

Commit

Permalink
Refactor server
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Dec 24, 2017
1 parent 3439b96 commit 7f2d690
Showing 1 changed file with 36 additions and 41 deletions.
77 changes: 36 additions & 41 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,45 @@ if (argv.version || argv.v) {
// Start radio
rtlsdr.start(argv)

// Start HTTP server
startServer()

function startServer () {
patterns.add('GET /', routes.index)
patterns.add('GET /assets/{file}', routes.assets)
patterns.add('GET /airlines', routes.airlines)
patterns.add('GET /airports', routes.airports)
patterns.add('GET /aircrafts', routes.aircrafts)

const server = http.createServer(function (req, res) {
debug('%s %s', req.method, req.url)

const path = url.parse(req.url).pathname
const match = patterns.match(req.method + ' ' + path)

if (!match) {
res.writeHead(404)
res.end()
return
}
patterns.add('GET /', routes.index)
patterns.add('GET /assets/{file}', routes.assets)
patterns.add('GET /airlines', routes.airlines)
patterns.add('GET /airports', routes.airports)
patterns.add('GET /aircrafts', routes.aircrafts)

const server = http.createServer(function (req, res) {
debug('%s %s', req.method, req.url)

const path = url.parse(req.url).pathname
const match = patterns.match(req.method + ' ' + path)

if (!match) {
res.writeHead(404)
res.end()
return
}

const fn = match.value // expects the value to be a function
req.params = match.params
const fn = match.value // expects the value to be a function
req.params = match.params

fn(req, res)
})
fn(req, res)
})

const customPort = argv.port || argv.p

if (customPort) listen(customPort)
else getPort({port: 3000}).then(listen)

function listen (port) {
server.listen(port, function () {
const url = 'http:https://localhost:' + port
if (argv.browser === false) {
console.log('Server running at: %s', url)
} else {
console.log('Opening %s in your favorite browser...', url)
opn(url)
}
})
}
const customPort = argv.port || argv.p

if (customPort) listen(customPort)
else getPort({port: 3000}).then(listen)

function listen (port) {
server.listen(port, function () {
const url = 'http:https://localhost:' + port
if (argv.browser === false) {
console.log('Server running at: %s', url)
} else {
console.log('Opening %s in your favorite browser...', url)
opn(url)
}
})
}

function help () {
Expand Down

0 comments on commit 7f2d690

Please sign in to comment.