Hi!
I want to run a Node.js server with database access in PureScript.
It should return a username from a database on http://localhost:8080/user.
I have used purescript-express and purescirpt-mysql. (express-example),(mysql-example)
Calling the server with:
runToDoServer :: Effect Unit
runToDoServer = do
conn <- liftEffect $ createConnection connectionInfo
state <- initState
port <- (parseInt <<< fromMaybe "8080") <$> lookupEnv "PORT"
_ <-
listenHttp (appSetup state conn) port \_ ->
log $ "Listening on " <> show port
liftEffect $ closeConnection conn
appSetup :: AppState -> Connection -> App
appSetup state conn = do
liftEffect $ log "Setting up"
setProp "json spaces" 4.0
use (logger state)
get "/user" (getUser conn)
useOnError (errorHandler state)
getFirstUser :: MySQL.Connection -> Aff (Array { nickname :: String })
getFirstUser conn = MySQL.query_ "SELECT nickname FROM users LIMIT 1" conn
processRows :: Array ({ nickname :: String }) -> Handler
processRows rows = case head rows of
Just user -> sendJson { status: user.nickname }
Nothing -> nextThrow $ error "No User"
getUser :: Connection -> Handler
getUser conn = do
fiberRows <- launchAff $ getFirstUser conn
rows <- joinFiber fiberRows
processRows rows
-- Error: Could not match type HandlerM with type Aff
If someone has a working example or knows how to solve this, I would be glad to hear.