Permalink
Browse files

Clean article image data before sending to client

  • Loading branch information...
1 parent f86c7e1 commit 4900e3f2a69aaead8566dd92ecd3c5c237cced6a Joshua Davis committed Apr 17, 2014
Showing with 12 additions and 3 deletions.
  1. +12 −3 server/resources/articles.js
@@ -6,6 +6,16 @@ var config = require('../../config.json')
var Article = mongoose.model("Article", articleSchema);
+function formatArticleImageData(result) {
+ //Return early if there is no article image
+ if (!result.articleImage) {
+ return result;
+ }
+ delete result.articleOriginalImageUrl;
+ result.articleImage = config.newsImageUrl + '/' + result.articleImage;
+ return result;
+}
+
module.exports.getArticles = function(req, res, next) {
var SORT_FIELDS = /upvotes|date|clicks|createdAt/;
@@ -54,9 +64,7 @@ module.exports.getArticles = function(req, res, next) {
mongoQuery.exec(function(error, results) {
results = results.map(function(result) {
- if (result.articleImage) {
- result.articleImage = config.newsImageUrl + '/' + result.articleImage;
- }
+ result = formatArticleImageData(result);
return result;
});
res.send(resultFormatter.multiResultsFormatter(results));
@@ -67,6 +75,7 @@ module.exports.getArticles = function(req, res, next) {
module.exports.getArticleById = function(req, res, next) {
Article.findById(req.params.id).exec(function(error, article) {
+ article = formatArticleImageData(article);
res.send(resultFormatter.singleResultFormatter(article));
});
}

0 comments on commit 4900e3f

Please sign in to comment.