API Atlas is an example of distant API provided with NodeAtlas.
`POST /api/comments/` (variable: message)
`GET http://www.lesieur.name/api/comments/`
/* jslint browser: true */
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.lesieur.name/api/comments/", true);
xhr.addEventListener("readystatechange", function () {
var commentsArea = document.getElementsByClassName("comments")[0],
comments;
if (xhr.readyState === 4 && xhr.status === 200) {
comments = JSON.parse(xhr.responseText);
comments.forEach(function (comment) {
var group = document.createElement("li"),
title = document.createElement("h4"),
paragraph = document.createElement("p");
title.textContent = comment.id;
paragraph.textContent = comment.message;
group.appendChild(title);
group.appendChild(paragraph);
commentsArea.appendChild(group);
});
}
});
xhr.send(null);
`GET http://www.lesieur.name/api/comments/`
/* jslint node: true, esversion: 6 */
exports.changeVariations = function (next, locals) {
var NA = this,
http = NA.modules.http,
result = "";
http.get({
hostname: "www.lesieur.name",
port: 80,
path: "/api/comments/",
agent: false
}, function (response) {
response.on("data", function (chunk) {
JSON.parse(chunk).forEach(function (comment) {
result += `<div>
<h4>
${comment.id}
</h4>
<p>
${comment.message}
</p>
</div>`;
});
locals.comments = result;
next();
});
}).on('error', function () {
locals.comments = "";
next();
});
};
Un message
Nouveau !!!
Tata
Titi