Making a Hexo.io tag that gets JSON data with an httprequest

I have written a post on hexo.io that outlines how to go about making a hexo tag that gets data from an async file read. Sometimes I might want to write a tag that gets data that is to be used to generate content in a page by way of an async http request. In this post I will be outline how I found a way to go about doing just that. However lately I have found that this is something that I should try to avoid doing actually. In think that hexo should just be used to build from a source folder, and maybe not have much to do with generating that source.

1 - Is it a good Idea

I am on the fence with this. As of late I like the idea of having separate scripts that can be used to update the actual text of my markdown files, rather than writing a hexo tag. Still I have not yet found, or developed a decent software solution for maintaining a large collection of markdown files. For now it would seem that this approach works okay.

So in the scripts folder of my hexo project working tree I have a *.js file called “my-tags.js” which is where I register all the hexo tags for use in my blog posts. In there I have this code that is relevant to this process.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var http = require('https'),
fs = require('fs'),
httpRequest = function (params, postData) {
return new Promise(function (resolve, reject) {
var req = http.request(params, function (res) {
// reject on bad status
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('statusCode=' + res.statusCode));
}
// cumulate data
var body = [];
res.on('data', function (chunk) {
body.push(chunk);
});
// resolve on end
res.on('end', function () {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch (e) {
reject(e);
}
resolve(body);
});
});
// reject on request error
req.on('error', function (err) {
// This is not a "Second reject", just a different sort of failure
reject(err);
});
if (postData) {
req.write(postData);
}
// IMPORTANT
req.end();
});
},
// log method for this file
log = function (mess) {
console.log('**********');
if (typeof mess != 'string') {
console.log('my-tags: non-string: ');
console.log(mess);
} else {
console.log('my-tags : ' + mess);
}
console.log('**********');
};
// async call to fixer
hexo.extend.tag.register('mytags_fixer', function (args) {
log('making a request...');
return httpRequest({
host : 'api.fixer.io',
method : 'GET',
path : '/latest'
}).then(function (data) {
log('request is good.');
// just assume the data is good and go for it, because I feel lucky.
var html = '<p>date of rates: ' + data.date + '<\/p>',
rate;
for (rate in data.rates) {
html += '<p>' + rate + ' : ' + data.rates[rate] + '<\/p>';
}
return html;
}).catch (function (err) {
log('bad request.');
log(err);
return '<p> Error getting data :( <\/p>';
});
}, {
async : true
});

So I am just registering a hexo tag, the only difference is that it is going to have to be an async tag because of the latency with making the request.

2 - conclusion

That is it for now be sure to check out my other posts on hexo