javascript - redirect image file get requests express 4.x and handlebars -
i have following routes:
index.js:
router.get('/', function(req, res, next) { var flag = "true"; var hflag = "true"; res.render('index',{title:"my blog",header: hflag, login: flag, categories:blogengine.getblogentries()}); }); router.get('/article/:id', function(req, res) { var entry = blogengine.getblogentry(req.params.id); var img = entry.img; res.render('article',{title:entry.title, gedjit:entry}); res.redirect(img); });
in '/' route, load list of images public static file folder 'images'. works fine until click on 1 of images. want show clicked image in new page.
here handlebars file;
article.hbs:
<h1>{{gedjit.title}}</h1> <div class="gedjit"> <img class="img" src="{{gedjit.img}}"> </div> <br> <p>description: {{gedjit.description}}</p>
the problem handlebars sending "localhost/article/images/source.jpeg" route instead of route "localhost/images/source.jpeg" in / route.
is there way redirect route , remove article request sent handlebars server?
i found out problem was.
firstly image source url did not have / in front of it; "images/sources.jpeg" instead of "/images/sources.jpeg"
secondly used 2 routes send partial , image file separately not can't set headers after sent error express.
the /articles/:id route looks this;
index.js
router.get('/article/:id', function(req, res) { var entry = blogengine.getblogentry(req.params.id); var img = entry.img; res.render('article',{title:entry.title, gedjit:entry}); res.sendfile(img); });
Comments
Post a Comment