Skip to content

Commit

Permalink
add Express Hello World example
Browse files Browse the repository at this point in the history
fixes: #122
  • Loading branch information
joelnet committed Oct 3, 2018
1 parent 11ee9cc commit 2f760e0
Show file tree
Hide file tree
Showing 8 changed files with 6,121 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ run ({ state, main })
### More Examples

- [Star Wars Console](examples/star-wars-console) - Check this out!
- [Express Hello World](examples/star-wars-console) - `express` Hello World.
- [Async Simple](examples/async-simple)
- [Hello World](examples/hello-world)
- [Conditionals](examples/conditionals)
Expand Down
3 changes: 3 additions & 0 deletions examples/express-hello-world/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": false
}
36 changes: 36 additions & 0 deletions examples/express-hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# MojiScript Express Hello World

App demonstrates how to interop with JavaScript libraries in a MojiScript application. In this case, we are interoping with `express` to create a simple web server that listens on port 3000.

## Install

```bash
# clone repository
git clone https://github.com/joelnet/MojiScript.git

# enter directory
cd MojiScript/examples/hello-world

# install dependencies
npm ci
```

## Run

```bash
npm start
```

You should see the output

```
Listening on port 3000.
```

## Code

[index.mjs](index.mjs) - Load dependencies and settings and start the application.

[main.mjs](main.mjs) - Entrypoint of application.

[interop/express.mjs](interop/express.mjs) - `express` JavaScript iterop.
15 changes: 15 additions & 0 deletions examples/express-hello-world/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logF from 'mojiscript/console/logF'
import run from 'mojiscript/core/run'
import express from './interop/express'
import main from './main'

const dependencies = {
express: express (),
logF
}

const state = {
port: 3000
}

run ({ dependencies, state, main })
17 changes: 17 additions & 0 deletions examples/express-hello-world/interop/express.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Interop file converts the JavaScript express library into a MojiScript compatible library.
*/
import express from 'express'

export default () => {
// wrap app in our own function so we can return MojiScript compatible `get` and `listen`.
const app = express()

return {
// curry `get` and `handler
get: path => handler => app.get(path, (req, res) => handler(req)(res)),

// convert callback into a Promise.
listen: port => new Promise(resolve => app.listen(port, data => resolve(data)))
}
}
17 changes: 17 additions & 0 deletions examples/express-hello-world/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pipe from 'mojiscript/core/pipe'
import tap from 'mojiscript/function/tap'
import $ from 'mojiscript/string/template'

const defaultRoute = () => res => res.send ('Hello World')
const startExpress = express => ({ port }) => express.listen (port)
const setupRoutes = express => pipe ([
() => express.get ('/') (defaultRoute)
])

const main = ({ express, logF }) => pipe ([
tap (setupRoutes (express)),
tap (startExpress (express)),
logF ($`Listening on port ${'port'}.`)
])

export default main

0 comments on commit 2f760e0

Please sign in to comment.