-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes: #122
- Loading branch information
Showing
8 changed files
with
6,121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.formatOnSave": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.