Comments (1)
I implemented it my way, created a new file template-storage.js
based on fetch-template.js
from tailor:
"use strict";
const http = require("http");
const url = require("url");
const TEMPLATE_ERROR = 0;
const TEMPLATE_NOT_FOUND = 1;
class TemplateError extends Error {
constructor(...args) {
super(...args);
this.code = TEMPLATE_ERROR;
this.presentable = "Template Error";
const [{ statusCode }] = args;
if (statusCode === 404) {
this.code = TEMPLATE_NOT_FOUND;
this.presentable = "Template Not Found";
}
}
}
/**
* Returns pathname by request
*
* @param {Object} request - Request Object
*
* @return {String} pathname
*/
const getPathName = (request) => url.parse(request.url, true).pathname;
/**
* Fetches the template from storage
*/
module.exports = () => async (request, parseTemplate) => {
const pathname = getPathName(request);
return parseTemplate(
await new Promise((resolve, reject) => {
const req = http.get(
`http://s3.example.com.br/templates${pathname}.html`
);
req.on("response", (res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
if (res.statusCode !== 200) {
reject(new TemplateError(res));
}
resolve(data);
});
});
req.on("error", (err) => {
console.log("Error: " + err.message);
reject(err);
});
})
);
};
module.exports.TEMPLATE_ERROR = TEMPLATE_ERROR;
module.exports.TEMPLATE_NOT_FOUND = TEMPLATE_NOT_FOUND;
and updated my index.js
:
"use strict";
const http = require("http");
const Tailor = require("node-tailor");
const TemplateStorage = require("./template-storage");
const PORT = process.env.PORT || 8080;
const tailor = new Tailor({
// templatesPath: __dirname + '/templates', // local templates only
fetchTemplate: TemplateStorage(),
});
http
.createServer((req, res) => {
if (req.url === "/favicon.ico") {
res.writeHead(200, { "Content-Type": "image/x-icon" });
return res.end("");
}
req.headers["x-request-uri"] = req.url;
tailor.requestHandler(req, res);
})
.listen(PORT, function () {
console.log(`Tailor server listening on port ${PORT}`);
})
.on("error", (req, err) => console.error(err));
from tailor.
Related Issues (20)
- What is the difference between Tags and Fragments? HOT 4
- Custom Tags wraps html HOT 1
- tailorjs nextjs alternative HOT 11
- How to dynamically add fragment at client end HOT 2
- React re-hydration for multiple fragments
- Use templates sent along with the request. HOT 4
- Send Authentication Header for Fragments HOT 5
- requestFragment type definition does not match implementation
- parseTemplate type definition does not match implementation
- Stop to reload fixed fragments in different route for application. HOT 4
- Custom Attribute passing in <fragment> not working since 3.8.0 HOT 1
- Need to change the response status code when rendered the fallback url.
- Fragment Attributes: Documentation doesn't match implementation HOT 1
- Possibility to switch between different environments
- Fix deprecated code and eslint
- Repository Status & Roadmap HOT 2
- Fragment-common, Vue 3 & RequireJS HOT 5
- Fragments Layout on a template
- Custom event during parsing template HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tailor.