Fay Node.js Hello example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE EmptyDataDecls #-} | |
module Hello where | |
import FFI | |
data Http | |
data HttpServer | |
data Request | |
data Response | |
requireHttp :: Fay Http | |
requireHttp = ffi "require('http')" | |
createServer :: Http -> (Request -> Response -> Fay ()) -> Fay HttpServer | |
createServer = ffi "%1.createServer(%2)" | |
consoleLog :: String -> Fay () | |
consoleLog = ffi "console.log(%1)" | |
listen :: HttpServer -> Int -> String -> Fay () | |
listen = ffi "%1.listen(%2, %3)" | |
writeHead :: Response -> Int -> String -> Fay () | |
writeHead = ffi "%1.writeHead(%2, %3)" | |
end :: Response -> String -> Fay () | |
end = ffi "%1.end(%2)" | |
main :: Fay () | |
main = do | |
http <- requireHttp | |
server <- createServer http (\req res -> do | |
writeHead res 200 "{ 'Content-Type': 'text/plain' }" | |
end res "Hello World\n" | |
) | |
listen server 1337 "127.0.0.1" | |
consoleLog "Server running at http://127.0.0.1:1337/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment