Transforms are now only cached until the source file changes, and the extension argument now expects a period (i.e ".html") to be consistent with the path.extname function.master
| ``` | ``` | ||||
| var sass = require("node-sass"); | var sass = require("node-sass"); | ||||
| app.transform("sass", "text/css", (path, writeStream) => { | |||||
| app.transform(".sass", "text/css", (path, writeStream) => { | |||||
| try { | try { | ||||
| var str = fs.readFileSync(path, "utf8"); | var str = fs.readFileSync(path, "utf8"); | ||||
| } catch (err) { | } catch (err) { | ||||
| Now, any .sass file served with `webframe.static` will be compiled into CSS, | Now, any .sass file served with `webframe.static` will be compiled into CSS, | ||||
| and sent with the browser with the MIME type "text/css". The result is also | and sent with the browser with the MIME type "text/css". The result is also | ||||
| cached, so the expensive sass compilation will only happen the first time the | cached, so the expensive sass compilation will only happen the first time the | ||||
| file is requested. The result will not be cached if you call writeStream.error. | |||||
| file is requested, or when the file changes. The result will not be cached | |||||
| if you call writeStream.error. |
| // Endpoint which uses the "params" middleware to parse URL parameters | // Endpoint which uses the "params" middleware to parse URL parameters | ||||
| app.get("/params-example", [ mw.params ], (req, res) => { | app.get("/params-example", [ mw.params ], (req, res) => { | ||||
| res.writeHead(200, { "Content-Type": "text/plain" }); | |||||
| res.end("Hello, "+req.params.name); | res.end("Hello, "+req.params.name); | ||||
| }); | }); | ||||
| // Endpoint which uses the "payload" middleware to get the payload as a string | // Endpoint which uses the "payload" middleware to get the payload as a string | ||||
| app.post("/payload-example", [ mw.payload ], (req, res) => { | app.post("/payload-example", [ mw.payload ], (req, res) => { | ||||
| res.writeHead(200, { "Content-Type": "text/plain" }); | |||||
| res.end(req.payload); | res.end(req.payload); | ||||
| }); | }); | ||||
| } | } | ||||
| var transformCache = {}; | var transformCache = {}; | ||||
| function handleTransform(path, req, res, app) { | |||||
| var ext = pathlib.extname(req.url).substring(1); | |||||
| function handleTransform(stat, path, req, res, app) { | |||||
| var ext = pathlib.extname(path).substring(1); | |||||
| if (ext === "") | if (ext === "") | ||||
| return false; | return false; | ||||
| if (transformCache[path]) { | if (transformCache[path]) { | ||||
| var c = transformCache[path]; | var c = transformCache[path]; | ||||
| res.writeHead(c.status, c.headers); | |||||
| res.end(c.content); | |||||
| return true; | |||||
| if (stat.ctime === c.ctime) { | |||||
| res.writeHead(c.data.status, c.data.headers); | |||||
| res.end(c.data.content); | |||||
| return true; | |||||
| } else { | |||||
| console.log("throwing out because ctime."); | |||||
| delete transformCache[path]; | |||||
| } | |||||
| } | } | ||||
| var data = { content: "", headers: null, status: null }; | var data = { content: "", headers: null, status: null }; | ||||
| if (!nocache) { | if (!nocache) { | ||||
| data.status = writeStream.status; | data.status = writeStream.status; | ||||
| data.headers = writeStream.headers; | data.headers = writeStream.headers; | ||||
| transformCache[path] = data; | |||||
| transformCache[path] = { | |||||
| data: data, | |||||
| ctime: stat.ctime | |||||
| }; | |||||
| } | } | ||||
| }, | }, | ||||
| // Join the web root with the request's path name | // Join the web root with the request's path name | ||||
| var path = pathlib.join(root, pn.replace(before, "")); | var path = pathlib.join(root, pn.replace(before, "")); | ||||
| // If there's a transform, let that handle it | |||||
| if (handleTransform(path, req, res, app)) | |||||
| return; | |||||
| // Send a file | // Send a file | ||||
| function send(path) { | function send(path) { | ||||
| sendfile(path, app, pn, req, res); | sendfile(path, app, pn, req, res); | ||||
| path = pathlib.join(path, "index.html"); | path = pathlib.join(path, "index.html"); | ||||
| } | } | ||||
| // Send the file | |||||
| // If there's a transform, let that handle it | |||||
| if (handleTransform(stat, path, req, res, app)) | |||||
| return; | |||||
| // Otherwise, send the file | |||||
| send(path); | send(path); | ||||
| }); | }); | ||||
| } | } |
| { | { | ||||
| "name": "webframe", | "name": "webframe", | ||||
| "version": "0.7.0", | |||||
| "version": "0.8.0", | |||||
| "description": "Web framework.", | "description": "Web framework.", | ||||
| "main": "index.js", | "main": "index.js", | ||||
| "scripts": { | "scripts": { |