Add 'hostname' config option, for serverHttp.listen() #642

Closed
wmhilton opened this Issue Sep 15, 2013 · 4 comments

Comments

Projects
None yet
3 participants

In Express, I could specify 'localhost' as the hostname to bind with, which meant the server would only listen for requests sent to 'localhost'.

app.listen(80,'localhost')

This was great because it blocked robots/hackers from connecting to my development "server" (a Win7 laptop) and potentially exploiting it, and I didn't have to specify a port number when typing URLs.

Now that I'm using Docpad, I would like to do the same thing by specifying 'localhost' somewhere in the docpad.coffee settings file. However, I can tell by looking in docpad/src/lib/docpad.coffee that the call to .listen() only passes a port number and a callback, not a hostname in line 3.

# Listen
docpad.log 'debug', util.format(locale.serverStart, port, config.outPath)
serverHttp.listen port,  ->
    # Log
    address = serverHttp.address()
    serverHostname = if address.address is '0.0.0.0' then 'localhost' else address.address
    serverPort = address.port
    serverLocation = "http://#{serverHostname}:#{serverPort}/"
    docpad.log 'info', util.format(locale.serverStarted, serverLocation, config.outPath)

    # Done
    return next()

Could you add support for a 'hostname' setting to docpad.coffee? I know I could hack my way around this by using my own Express instance but I feel like that is egregious overkill. (Edit: I spent a half hour trying to get that to work but couldn't.) I feel like this would be a useful feature for any developer who is hosting the docpad server and viewing the served pages on the same machine, but doesn't want to accidentally make that server accessible to anyone in the world who knows their IP address and default docpad port number.


Want to back this issue? Place a bounty on it! We accept bounties via Bountysource.

Thought I'd take a crack at adding the feature myself. This was all tested on my local machine, because really I'm just using docpad as a static site generator for Github Pages. Here's what I used to test it in my docpad.coffee:

  5 docpadConfig = {
...
 60   environments:
 61     development:
 62       hostname: 'localhost'     # http://localhost/ works
 63                                 # http://mywebsite.com/ doesn't
 64     production:
 65       hostname: 'mywebsite.com' # http://mywebsite.com/ works
 66                                 # http://localhost/ doesn't
 65 }

mywebsite.com is not actually my website's name. I used a URL that resolved to my public IP address.

Also, I wouldn't actually recommend using a 'hostname' option in production, because in production I don't think you would care how hostname was resolved.

Let me know what you think. As a new config option, it would have to be documented. I would also recommend setting hostname: 'localhost' for the default development environment, because I'm guessing the most common use case is people running docpad and viewing the pages from the same machine. In that case, I'd prefer to protect them by default, since they may not realize they're exposing their http server to the Internet. Weighed against that is the people who are running docpad on a development server and looking at the pages on their local machine. They may be terribly upset that they have to specify hostname: null in their development environment config.

Contributor

radiodario commented Dec 15, 2013

I'm just wondering, there are cases where listening on 0.0.0.0 is a good idea (i.e. on my development machine, when doing mobile testing, etc). At the moment this is not possible, as it always reverts to localhost - unless I set my local ip address, which might change at the routers will...

Owner

balupton commented Dec 16, 2013

Will happily accept a pull request for this.

Contributor

radiodario commented Dec 16, 2013

Ok - I'm testing this and actually I think it is listening on 0.0.0.0 by default, but displaying localhost, is that right? I've added some changes so that you can specify a hostname at the same level as port. I'll add a pull request now.

radiodario referenced this issue Dec 16, 2013

Merged

added hostname configuration #737

2 of 2 tasks complete

wmhilton closed this Oct 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment