diff --git a/lib/models/server/app-api.js b/lib/models/server/app-api.js index 00a1c46..b373496 100644 --- a/lib/models/server/app-api.js +++ b/lib/models/server/app-api.js @@ -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."); + }); + } }; }; diff --git a/lib/models/server/index.js b/lib/models/server/index.js index bdd88bd..ea638af 100644 --- a/lib/models/server/index.js +++ b/lib/models/server/index.js @@ -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));