Skip to content

Commit 0020da1

Browse files
authored
Merge pull request #12 from purescript-node/listen-hostname
Allow specifying a hostname with Node.HTTP.listen
2 parents 1bae2f6 + 7042daa commit 0020da1

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

src/Node/HTTP.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,29 @@ exports.createServer = function (handleRequest) {
1010
};
1111
};
1212

13-
exports.listen = function (server) {
13+
exports.listenImpl = function (server) {
1414
return function (port) {
15+
return function (hostname) {
16+
return function (backlog) {
17+
return function (done) {
18+
return function () {
19+
if (backlog !== null) {
20+
server.listen(port, hostname, backlog, done);
21+
} else {
22+
server.listen(port, hostname, done);
23+
}
24+
};
25+
};
26+
};
27+
};
28+
};
29+
};
30+
31+
exports.listenSocket = function (server) {
32+
return function (path) {
1533
return function (done) {
1634
return function () {
17-
server.listen(port, function () {
18-
done();
19-
});
35+
server.listen(path, done);
2036
};
2137
};
2238
};

src/Node/HTTP.purs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Prelude
66

77
import Control.Monad.Eff (Eff)
88

9+
import Data.Maybe (Maybe)
10+
import Data.Nullable (Nullable, toNullable)
911
import Data.StrMap (StrMap)
1012

1113
import Node.Stream (Writable, Readable)
@@ -27,8 +29,21 @@ foreign import data HTTP :: !
2729
-- | Create a HTTP server, given a function to be executed when a request is received.
2830
foreign import createServer :: forall eff. (Request -> Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Server
2931

30-
-- | Listen on the specified port. The specified callback will be run when setup is complete.
31-
foreign import listen :: forall eff. Server -> Int -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
32+
foreign import listenImpl :: forall eff. Server -> Int -> String -> Nullable Int -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
33+
34+
-- | Listen on a port in order to start accepting HTTP requests. The specified callback will be run when setup is complete.
35+
listen :: forall eff. Server -> ListenOptions -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
36+
listen server opts done = listenImpl server opts.port opts.hostname (toNullable opts.backlog) done
37+
38+
-- | Listen on a unix socket. The specified callback will be run when setup is complete.
39+
foreign import listenSocket :: forall eff. Server -> String -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
40+
41+
-- | Options to be supplied to `listen`. See the [Node API](https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_server_listen_handle_callback) for detailed information about these.
42+
type ListenOptions =
43+
{ hostname :: String
44+
, port :: Int
45+
, backlog :: Maybe Int
46+
}
3247

3348
-- | Get the request HTTP version
3449
httpVersion :: Request -> String

test/Main.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log, logShow)
77

88
import Data.Foldable (foldMap)
9+
import Data.Maybe (Maybe(..))
910

1011
import Node.Encoding (Encoding(..))
1112
import Node.HTTP (HTTP, listen, createServer, setHeader, requestMethod, requestURL, responseAsStream, requestAsStream, setStatusCode)
@@ -25,7 +26,7 @@ main = do
2526
testBasic :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
2627
testBasic = do
2728
server <- createServer respond
28-
listen server 8080 $ void do
29+
listen server { hostname: "localhost", port: 8080, backlog: Nothing } $ void do
2930
log "Listening on port 8080."
3031
simpleReq "http://localhost:8080"
3132
where

0 commit comments

Comments
 (0)