I have a node.js script in which I make a rest request (a GET request in this case). The output shows up in the console just fine when I run the script from the command line. I want to display the output to an html page (in other words - I want to eventually run this script from web web page).
I have created two node.js scripts - one for the rest request and one to direct the output to an html page.
rest request script:
const request = require('request-promise')
const readline = require('readline')
const options = {
method: 'GET',
uri: 'http://dummy.restapiexample.com/api/v1/employees'
}
request(options)
.then(function (response) {
// Request was successful, use the response object at will
json: true
//console.dir(response)
JSON.parse(response)
console.dir((JSON.parse(response)), {depth: null, colors: true})
})
.catch(function (err) {
// Something bad happened, handle the error
})
let currentResult = request
outputResult(currentResult);
function outputResult(result) {
currentResult = (request);
}
output director to html page:
const restProto = document.getElementById('protocol');
const apiToTest = document.getElementById('uri');
const testApiResult = document.getElementById('output');
const currentResultOutput = document.getElementById('output');
function outputResult(result) {
currentResultOutput.textContent = result;
}
Here is the html page I am trying to direct to:
<!DOCTYPE html>
<html>
<body>
<head>
<meta name="viewport" content="width=device-width" initial-scale=1" charset="UTF-8">
<style>
.collapsible {
background-color: white;
color: black;
cursor: pointer;
padding: 1px;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.active, .collapsible:hover {
background-color: #f1f1f1;
}
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #f1f1f1;
}
</style>
</head>
<h2>API Test (v 0.1 alpha)</h2>
<form>
<label for="protocol">protocol</label>
<select name="restcall" id="protocol">
<option value="get">GET</option>
<option value="put">PUT</option>
<option value="post">POST</option>
<option value="delete">DELETE</option>
</select>
<label for="uri"> url: </label>
<input type="text" id="uri" name="uri">
<br><br>
<button class="collapsible">Advanced</button>
<div class="content">
<br>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.</p><br>
</div>
<br><br>
<input type="submit" value="Send">
<br><br>
</form>
<textarea id="output" name="output" rows="20" cols="50">
Response displays here ...
</textarea>
<script>
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
//this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}
</script>
<script src="./apitest2.js"></script>
<script src="./apitest3.js"></script>
<script src="./responseDirector.js"></script>
</body>
</html>
What am I missing to make this code work?