# Backend apps with Elm: elm-express

**URL:** <https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088>\
**Category:** Show and Tell\
**Created:** [April 18, 2023, 6:04pm UTC](https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088 "2023-04-18T18:04:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![eberfreitas](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eberfreitas/32/2741_2.png) [@eberfreitas](https://discourse.elm-lang.org/u/eberfreitas)\
**Post date:** [April 18, 2023, 6:04pm UTC](https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088/1 "2023-04-18T18:04:26Z")

</div>

Hello folks.

I released a new library/package called elm-express:

[https://package.elm-lang.org/packages/eberfreitas/elm-express/latest/](https://package.elm-lang.org/packages/eberfreitas/elm-express/latest/)

It is an attempt of creating something that allows me to write backend apps with Elm. I wrote a little bit about why I did it here:

> **[Announcing elm-express](https://dev.to/eberfreitas/announcing-elm-express-4hjn)**
>
> Not knowing it was impossible, he went there and discovered it. Elm in the backend has always been...

Would love to hear your thoughts about it if there are any. Thanks!

---

<div class="post-metadata">

**Author:** ![r4z4](https://avatars.discourse-cdn.com/v4/letter/r/aeb1de/32.png) [@r4z4](https://discourse.elm-lang.org/u/r4z4)\
**Post date:** [April 21, 2023, 3:52am UTC](https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088/2 "2023-04-21T03:52:27Z")

</div>

Very excited to give this a try 🙂 Time to dust off the old notes. Thanks for your efforts on this. Looks great.

---

<div class="post-metadata">

**Author:** ![lpil](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lpil/32/3139_2.png) [@lpil](https://discourse.elm-lang.org/u/lpil)\
**Post date:** [April 21, 2023, 3:39pm UTC](https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088/3 "2023-04-21T15:39:36Z")

</div>

Very cool! Are there examples for how to do things like read data from a file, database, or some other form of persistence? For example fetching a user’s profile. Thanks!

---

<div class="post-metadata">

**Author:** ![eberfreitas](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eberfreitas/32/2741_2.png) [@eberfreitas](https://discourse.elm-lang.org/u/eberfreitas)\
**Post date:** [April 21, 2023, 6:28pm UTC](https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088/4 "2023-04-21T18:28:11Z")

</div>

Not directly, but on the example app you have two examples for using ports and tasks:

- Port example
- 
  - [When the request arrives](https://github.com/eberfreitas/elm-express/blob/main/example/src/Main.elm#L98)

- 
  - [When we run the update function to respond to the port interaction](https://github.com/eberfreitas/elm-express/blob/main/example/src/Main.elm#L236)

- Task example
- 
  - [When the request arrives](https://github.com/eberfreitas/elm-express/blob/main/example/src/Main.elm#L150)

- 
  - [When we run the update function to respond to the task interaction](https://github.com/eberfreitas/elm-express/blob/main/example/src/Main.elm#L249)

You could use the task to make API requests to fetch user data, or ports/subscriptions if your Node.js app has database access.

Edit:

I would favor Tasks as they are easier to use with the library. I’m almost sure we can use something like [taskport](https://package.elm-lang.org/packages/lobanov/elm-taskport/latest/) to hack for a “better” ffi. Just didn’t want to include that into the framework.

---

<div class="post-metadata">

**Author:** ![Atlewee](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/atlewee/32/4603_2.png) [@Atlewee](https://discourse.elm-lang.org/u/Atlewee)\
**Post date:** [April 22, 2023, 10:06pm UTC](https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088/5 "2023-04-22T22:06:13Z")

</div>

Tried it out now. This is a really cool package. 🙂  
I tested creating a simple in memory CRUD API.  
(Lamdera-style: Persist state via port and load state via flags on startup)  
I defined something like this: type alias Model = { projects: Dict String Project } and gave this as ctx to the Application.  
( I renamed the “Connection-Model” from the example to “Endpoint” to avoid nameClash )

If someone POST to the “/projects” endpoint I would like to add a new project in that shared model and return the new id in the response. (Using the elm model as a databse, no task or port needed)  
But there seems to be no way to update the shared model (ctx) ?  
Is there any way to have any application state in elm? Or does it have to be on the JS side?  
❓ 🙂

---

<div class="post-metadata">

**Author:** ![eberfreitas](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eberfreitas/32/2741_2.png) [@eberfreitas](https://discourse.elm-lang.org/u/eberfreitas)\
**Post date:** [April 23, 2023, 3:14am UTC](https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088/6 "2023-04-23T03:14:33Z")

</div>

Thank you so much for trying it out @Atlewee . Really appreciate it 😊

> [@Atlewee](#):
>
> But there seems to be no way to update the shared model (ctx) ?

That is right. Currently the `ctx` is read only. I wasn’t sure which way to go there and I thought it would be best to remove restrictions in the future than adding one.

My thinking there was that `ctx` should be used for global read-only immutable things, like env vars and stuff. Also, I wanted to keep the return data from `incoming` and `update` pretty simple, but maybe `ctx` can be a part of `Conn`.

Another thing is that I’m not sure if allowing for the change of some global cross requests state might create problems with data being overwritten with older versions or race conditions.

I can definitly see why we would want a global shared state among requests… Need to think a little bit harder about it. What are your thoughts?

---

<div class="post-metadata">

**Author:** ![nateabele](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/nateabele/32/1504_2.png) [@nateabele](https://discourse.elm-lang.org/u/nateabele)\
**Post date:** [April 24, 2023, 4:05pm UTC](https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088/7 "2023-04-24T16:05:49Z")

</div>

Nice work! The API looks really good. I’ll have to give it a try soon.

---

<div class="post-metadata">

**Author:** ![eberfreitas](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eberfreitas/32/2741_2.png) [@eberfreitas](https://discourse.elm-lang.org/u/eberfreitas)\
**Post date:** [April 29, 2023, 8:50pm UTC](https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088/8 "2023-04-29T20:50:11Z")

</div>

Tks @nateabele

I would love to get some feedback 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [May 9, 2023, 8:50pm UTC](https://discourse.elm-lang.org/t/backend-apps-with-elm-elm-express/9088/9 "2023-05-09T20:50:58Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
