Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from movableink/mn/serve-compiled-app
Browse files Browse the repository at this point in the history
Serve compiled app
  • Loading branch information
mnutt authored Mar 13, 2018
2 parents 31aa6fc + 58a345a commit 64fd1ed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
39 changes: 32 additions & 7 deletions lib/models/server/app-api.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
const fs = require("fs");
const path = require("path");
const { promisify } = require("util");

const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

function sendContent(res, content) {
res.writeHead(200, {
"content-type": "text/html",
"X-XSS-Protection": 0,
"content-length": content.length
});
res.end(content);
}

module.exports = function(root) {
const tmpFilePath = path.join(root, 'dist', '.compiled-index.html');

return function(req, res) {
let content = req.body.content || "";
res.writeHead(200, {
"content-type": "text/html",
"X-XSS-Protection": 0,
"content-length": content.length
});
res.end(content);
if (req.method === "POST") {
let content = req.body.content || "";
writeFile(tmpFilePath, content).then(() => {
sendContent(res, content);
});
} else {
console.log("HERE");
readFile(tmpFilePath).then((content) => {
sendContent(res, content);
}).catch((e) => {
res.writeHead(500, {});
res.end("You need to have the Preview panel open at the same time in order for this page to work.");
});
}
};
};
6 changes: 3 additions & 3 deletions lib/models/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ module.exports = function(root, liveReloadPort) {
server.use(bodyParser.urlencoded({ extended: false, limit: '20mb' }));
server.use(bodyParser.json({limit: '20mb'}));

server.use("/app/dist", express.static(path.join(root, "/dist")));
server.use("/app", express.static(path.join(root, "/app")));
server.get("/app", appApi(root));
server.post("/app", appApi(root));
server.get("/app", (req, res) => res.redirectTo('/app/'));
server.use("/app/dist", express.static(path.join(root, "/dist")));
server.use("/app/", express.static(path.join(root, "/app")));

server.put("/api/canvas/manifests/0", saveApi(root));

Expand Down

0 comments on commit 64fd1ed

Please sign in to comment.