Skip to content

Commit

Permalink
call service from html interface
Browse files Browse the repository at this point in the history
  • Loading branch information
thongxuan committed Jun 2, 2015
1 parent 2139eb6 commit 7904068
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 9 deletions.
121 changes: 120 additions & 1 deletion test/html/service.html
Original file line number Diff line number Diff line change
@@ -1 +1,120 @@
THONG
<html>
<head>
<title>Test KeeeX API</title>
<script>
var http = require('http');

//call service and pass returned values to callback function
function callService(address, port, action, path, callback, errorhandler) {
var request="/keeex?action="+action+"&path="+path;
var options = {
host: address,
port: port,
path: request
};
var result = http.get(options, function(response)
{
var returnCode = response.statusCode;
var returnValue = "";
response.on('data', function(chunk)
{
returnValue+=chunk;
});

response.on('end', function() {
callback(returnCode, returnValue);
});
});

result.on("error", errorhandler);
}

//callback function to proceed returned values
function proceedResult(code, value)
{
document.getElementById("txtReturnCode").value=code;
document.getElementById("txtReturnValue").value=value;
}

//this function to handler error in case of not responding
function errorHandler()
{
document.getElementById("txtReturnCode").value="Cant connect to server";
document.getElementById("txtReturnValue").value="Cant connect to server";
}
//

//call service with arguments from html
function test()
{
var action = document.getElementById("optAction").value;
var path = document.getElementById("file").value;
var address = document.getElementById("txtHostAddress").value;
var port = document.getElementById("txtPort").value;
callService(address, port, action, path, proceedResult, errorHandler);
}
</script>
</head>
<body>
<table>
<tr>
<td>
Host address
</td>
<td>
<input id="txtHostAddress" type="text" value="127.0.0.1"/>
</td>
</tr>
<tr>
<td>
Port
</td>
<td>
<input id="txtPort" type="text" value="3000"/>
</td>
</tr>
<tr>
<td>
Action
</td>
<td>
<select id="optAction">
<option value="keeexit">Keeexit</option>
<option value="verify">Verify last version</option>
</select>
</td>
</tr>
<tr>
<td>
File path
</td>
<td>
<input id="file" type="file"/>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:right">
<input type="button" value="Test" onclick="test()"/>
</td>
</tr>
<hr/>
<tr>
<td>
Return code
</td>
<td>
<input id="txtReturnCode" type="text" editable="false"/>
</td>
</tr>
<tr>
<td>
Return value
</td>
<td>
<input id="txtReturnValue" type="text" editable="false"/>
</td>
</tr>
</table>
</body>

</html>
12 changes: 8 additions & 4 deletions test/js/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,43 @@ var server = http.createServer(function(request, response) {
response.writeHead(200);
if (action=="keeexit")
{

if (!path && fs.existsSync(path))
if (path && fs.existsSync(path))
{
response.writeHead(200);
/*TODO
return the path to the keeexed file or its content
*/
*/
response.write("This must be path to the keeexed file");
}
else
{
//return bad request
response.writeHead(400);
response.write("bad argument for keeex");
}
}
else if (action=="verify")
{
if (!path && fs.existsSync(path))
if (path && fs.existsSync(path))
{
response.writeHead(200);
/*TODO
return whether the file is in the lastest version
*/
response.write("This must be true or false");
}
else
{
//return bad request
response.writeHead(400);
response.write("bad argument for verify");
}
}
else
{
//action not supported
response.writeHead(404);
response.write("action not supported");
}
response.end();

Expand Down
4 changes: 0 additions & 4 deletions test/js/test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
var http = require('http');


function getStatusCode(request, callback) {
var options = {
host: "127.0.0.1",
port: 3000,
path: request,
headers: {
Host: request
}
};
http.get(options, function(response) {
callback(response.statusCode);
Expand Down

0 comments on commit 7904068

Please sign in to comment.