If you need to run Elm on server-side, check one of the following implementations:
- nomalab/elm-hapi (based on Hapi.js)
- elm-express (based on Express, coming soon)
- elm-fastify (based on fastify, coming soon)
To ease syntax and boilerplate and improve user experience, this lib can only run one server at a time per Elm worker. This is why you can only start once or again after stopping (start task will fail if you call it again) and you do not have to specify which server you are listening to when subscribing.
module YouCustomServer exposing (..)
import Json.Decode exposing (Decoder)
import Server
type alias Options = {
-- You should put here any options needed to create a raw server
}
create: Options -> Task Error Server.Server
create options =
{ implementation = Native.YouCustomServer.create options
, request =
{ decoder = requestDecoder
}
-- other stuff...
}
requestDecoder: Decoder Server.Request
requestDecoder =
-- you custom request decoder
You can put whatever you want in your
function start({
onRequest: function (replier, request) { /* elm-server internals */ }
}) {
// return Promise<ServerImplementation>
}
function stop() {
// return Promise
}
This will be fixed on Elm 0.19 but as far as 0.18 go, if a Json.Decode.Decoder
fails, it will call JSON.stringify
on the value it tried to decode. Since it is JavaScript, the value might be recursive and it will crash at runtime. To prevent that, you need to add a toJSON
function on any opaque type (ServerImplementation
, Replier
) and your request object before parsing it.
This software is licensed under the Apache 2 license, quoted below.
Copyright Paul Dijou (http://pauldijou.fr).
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.