-
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.
Merge pull request #144 from joelnet/story/example-express-static-files
add express static file server
- Loading branch information
Showing
11 changed files
with
6,162 additions
and
1 deletion.
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
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,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. |
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,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))) | ||
} | ||
} |
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,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 |
Oops, something went wrong.