You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
Hello, I'm trying to consume templates from a storage, but I'm only getting the response 'template not found'.
'use strict'consthttp=require('http')constTailor=require('node-tailor')consttailor=newTailor({templatesPath: 'https://s3.example.com.br'+'/templates'})http.createServer((req,res)=>{if(req.url==='/favicon.ico'){res.writeHead(200,{'Content-Type': 'image/x-icon'})returnres.end('')}req.headers['x-request-uri']=req.urltailor.requestHandler(req,res)}).listen(8080,function(){console.log('Tailor server listening on port 8080')})
I implemented it my way, created a new file template-storage.js based on fetch-template.js from tailor:
"use strict";consthttp=require("http");consturl=require("url");constTEMPLATE_ERROR=0;constTEMPLATE_NOT_FOUND=1;classTemplateErrorextendsError{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 */constgetPathName=(request)=>url.parse(request.url,true).pathname;/** * Fetches the template from storage */module.exports=()=>async(request,parseTemplate)=>{constpathname=getPathName(request);returnparseTemplate(awaitnewPromise((resolve,reject)=>{constreq=http.get(`http://s3.example.com.br/templates${pathname}.html`);req.on("response",(res)=>{letdata="";res.on("data",(chunk)=>(data+=chunk));res.on("end",()=>{if(res.statusCode!==200){reject(newTemplateError(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";consthttp=require("http");constTailor=require("node-tailor");constTemplateStorage=require("./template-storage");constPORT=process.env.PORT||8080;consttailor=newTailor({// templatesPath: __dirname + '/templates', // local templates onlyfetchTemplate: TemplateStorage(),});http.createServer((req,res)=>{if(req.url==="/favicon.ico"){res.writeHead(200,{"Content-Type": "image/x-icon"});returnres.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));
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Hello, I'm trying to consume templates from a storage, but I'm only getting the response 'template not found'.
There is any example or documentation about this?
Local directory works:
The text was updated successfully, but these errors were encountered: