Skip to content

Instantly share code, notes, and snippets.

@kofigumbs
Created March 29, 2018 23:19
Show Gist options
  • Save kofigumbs/44c728d9c0face4e1b8dcf125cd08277 to your computer and use it in GitHub Desktop.
Save kofigumbs/44c728d9c0face4e1b8dcf125cd08277 to your computer and use it in GitHub Desktop.
A tiny express server for compiling Elm
// 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}...`);
}); 
{
"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