Skip to content

Instantly share code, notes, and snippets.

@kseo

kseo/Hello.hs

Created Mar 11, 2014
Embed
What would you like to do?
Fay Node.js Hello example
{-# 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