-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adapter for express js middleware #5
Comments
Hi @srghma, Yes, it's actually a great idea! warp is built as a handler for wai, so as long as you can get the express middleware to match the I will try to look more into it later on. |
@srghma Sorry it took me a bit of time to get on this issue. I was focusing more on my other library (purescript-swerve). So, I implemented a solution that can allow you to add express middleware using Here is a working example using express static. Main.jsconst serveStatic = require('serve-static')
exports._serveStatic = serveStatic('public') Main.pursimport Prelude
import Data.Maybe (Maybe(..))
import Data.Tuple.Nested ((/\))
import Data.Vault as V
import Effect (Effect)
import Effect.Class.Console as Console
import Network.HTTP.Types (ok200)
import Network.HTTP.Types.Header (hContentType)
import Network.Wai (Application, Middleware, responseStr)
import Network.Warp.FFI.Server (ForeignApplication, HttpHandles, httpHandlesKey, mkMiddlewareFromForeign)
import Network.Warp.Run (runSettings)
import Network.Warp.Settings (defaultSettings)
main :: Effect Unit
main = do
let beforeMainLoop = Console.log $ "Listening on port " <> show defaultSettings.port
httpKey' <- httpHandlesKey -- creates key that will allow the middleware to access node HTTP request and response object
void $ runSettings defaultSettings { beforeMainLoop = beforeMainLoop, httpKey = Just httpKey' }
$ serverStatic httpKey'
$ app
app :: Application
app req f = do
f $ responseStr ok200 [(hContentType /\ "text/plain")] "Hello World!"
serverStatic :: V.Key HttpHandles -> Middleware
serverStatic k = mkMiddlewareFromForeign k _serveStatic
foreign import _serveStatic :: ForeignApplication Let me know if you need more details. I will be working on polishing |
My PR simplifies this even further, and mkMiddlewareFromForeign :: ForeignMiddleware -> Middleware The main :: Effect Unit
main =
void
$ runSettings defaultSettings
$ serveStatic "public"
$ app
app :: Application
app _ send = send $ responseStr ok200 [(hContentType /\ "text/plain")] "Hello World!"
foreign import _serveStatic :: String -> ForeignMiddleware
serveStatic :: String -> Middleware
serveStatic = mkMiddlewareFromForeign <<< _serveStatic |
For example hyper has
mkMiddlewareFromForeign
methodpurescript-hyper/hyper#47 (comment)
Is the same possible with warp?
The text was updated successfully, but these errors were encountered: