Created
March 29, 2018 23:19
-
-
Save kofigumbs/44c728d9c0face4e1b8dcf125cd08277 to your computer and use it in GitHub Desktop.
A tiny express server for compiling Elm
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
// JSON API | |
const app = require("express")(); | |
const bodyParser = require("body-parser"); | |
app.use(bodyParser.json()) | |
// COMPILE | |
const assert = require("assert"); | |
const fs = require("fs"); | |
const temp = require("temp"); | |
const elm = require("node-elm-compiler"); | |
const flags = { warn: true, yes: true, report: "json" }; | |
app.post("/compile", function (request, response) { | |
temp.open({ suffix: 'elm' }, (tempError, { fd, path }) => { | |
assert(!tempError); | |
fs.write(fd, request.body.elm, fsError => { | |
assert(!fsError); | |
elm.compileToString(path, flags).then( | |
output => response.send({ output }), | |
error => response.send({ error }) | |
); | |
}); | |
}); | |
}); | |
// START SERVER | |
const port = process.env["PORT"] || 3000; | |
app.listen(port, function() { | |
console.log(`Listening on port ${port}...`); | |
}); |
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
{ | |
"description": "A tiny express server for compiling Elm", | |
"repository": "gist.github.com/hkgumbs", | |
"license": "BSD3", | |
"dependencies": { | |
"body-parser": "^1.18.2", | |
"express": "^4.16.3", | |
"node-elm-compiler": "^4.5.0", | |
"npm": "^5.8.0", | |
"temp": "^0.8.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment