Skip to content

Commit

Permalink
Merge 36187a3 into b75700c
Browse files Browse the repository at this point in the history
  • Loading branch information
joelnet committed Oct 8, 2018
2 parents b75700c + 36187a3 commit 0868c24
Show file tree
Hide file tree
Showing 11 changed files with 6,162 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ run ({ state, main })

- [Star Wars Console](examples/star-wars-console) - Check this out!
- [Express Hello World](examples/express-hello-world) - Express "Hello World" web server.
- [Express Static File Server](examples/express-static-files) - Express static file server.
- [Async Simple](examples/async-simple)
- [Hello World](examples/hello-world)
- [Conditionals](examples/conditionals)
Expand Down
2 changes: 1 addition & 1 deletion examples/express-hello-world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ App demonstrates how to interop with JavaScript libraries in a MojiScript applic
git clone https://github.com/joelnet/MojiScript.git

# enter directory
cd MojiScript/examples/hello-world
cd MojiScript/examples/express-hello-world

# install dependencies
npm ci
Expand Down
3 changes: 3 additions & 0 deletions examples/express-static-files/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": false
}
38 changes: 38 additions & 0 deletions examples/express-static-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# MojiScript Express Static File Server

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 and serves static files in the public directory.

## Install

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

# enter directory
cd MojiScript/examples/express-static-files

# install dependencies
npm ci
```

## Run

```bash
npm start
```

You should see the output

```
Listening on port 3000.
```

Open http://localhost:3000 in your web browser.

## 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-static-files/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 })
24 changes: 24 additions & 0 deletions examples/express-static-files/interop/express.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Interop file converts the JavaScript express library into a MojiScript compatible library.
*/
import express from 'express';
import serveIndex from 'serve-index';

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

return {
// directory to call serveIndex
directory: path => serveIndex(path),

// pass through express.static
static: express.static,

// curry use
use: route => path => app.use(route, path),

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

// selectors
const getPort = ({ port }) => port

// express
const setupRoutes = express => pipe ([
() => express.use ('/') (express.static ('public')),
() => express.use ('/') (express.directory ('public'))
])
const startExpress = ({ listen }) => pipe ([
getPort,
listen
])

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

export default main
Loading

0 comments on commit 0868c24

Please sign in to comment.