Skip to content

Commit

Permalink
Call server close only once
Browse files Browse the repository at this point in the history
Some browser will try to download favicon.ico as a second request.
After the first request the server will be closed, so the request to the
favicon.ico would generate errors in the terminal.
The patch will set a variable which value will chang after server close
called, so next handler will not be called.
  • Loading branch information
Ajnasz committed Dec 24, 2013
1 parent 88cf9ba commit 13a3e54
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 37 deletions.
29 changes: 19 additions & 10 deletions index.coffee
Expand Up @@ -25,29 +25,38 @@ module.exports = (opts) ->
if arguments.length is 2
callback = openURICallback
openURICallback = null

# default: open the system's web browser
openURICallback or= (uri, cb) ->
#TODO: Make OS agnostic w/ xdg-open, open, etc.
exec "open '#{uri}'", cb

qs =
response_type: "code"
client_id: opts.client_id
redirect_uri: opts.redirect_uri
scope: scope

uri = endpoint_auth + "?" + querystring.stringify(qs)

console.log "Starting server ..."

opened = true

server = require("http").createServer((req, res) ->
console.log "server receives request for", req.url
console.log "Stopping server ..."
server.close()
res.end "ok"
callback null, querystring.parse(req.url.split("?")[1]).code
if opened
console.log "server receives request for", req.url
console.log "Stopping server ..."
server.close()
res.end "ok"

code = querystring.parse(req.url.split("?")[1]).code
if code
opened = false
callback null, code

).listen(3000)

openURICallback uri, (err) ->
if (err) then callback err
console.log "uri opened ..."
Expand Down
62 changes: 35 additions & 27 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 13a3e54

Please sign in to comment.