将检索到的aws实例显示到Node.js中的HTML页面

时间:2015-10-15 12:18:19

标签: javascript html node.js amazon-web-services amazon-ec2

我有一个从aws

中检索实例的代码
var aws = require('aws-sdk');
    aws.config.update({
        accessKeyId: 'YOUR_ACCESS_KEY', 
        secretAccessKey: 'YOUR_SECRET_KEY', 
        region: 'us-west-2'
    });
    var ec2 = new aws.EC2();
    function printStatuses() {
        ec2.describeInstances({}, function(err, data) {
            if(err) {
                console.error(err.toString());
            } else {
                var currentTime = new Date();
                console.log(currentTime.toString());

                for(var r=0,rlen=data.Reservations.length; r<rlen; r++) {
                    var reservation = data.Reservations[r];
                    for(var i=0,ilen=reservation.Instances.length; i<ilen; ++i) {
                        var instance = reservation.Instances[i];

                        var name = '';
                        for(var t=0,tlen=instance.Tags.length; t<tlen; ++t) {
                            if(instance.Tags[t].Key === 'Name') {
                                name = instance.Tags[t].Value;
                            }
                        }
                        console.log('\t'+name+'\t'+instance.InstanceId+'\t'+instance.PublicIpAddress+'\t'+instance.InstanceType+'\t'+instance.ImageId+'\t'+instance.State.Name);
                    }
                }
            }
        });    
    } 

上面的代码工作正常,检索实例并显示在终端中,我想在前面的HTML页面上显示它。我已经开发了前端页面,并且希望在此页面(内部表格标签)中显示实例而不是控制台。

front end html code

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

</script>
<meta charset="ISO-8859-1">
<style>
th {background-color:lightgrey;
    }
 h1 {color:blue;
     text-align:center;}
 p  {color:blue;
     text-align:left;}

</style>
</head>
<body>
<form action="/" method="post">
<table width="100%" border=1>
        <tr>
        <th>Instances</th>
        <th>Status</th>
        <th>Start/Stop</th>
    </tr>
        <tr>
        <td id="instance"></td>
        <td id="status"></td>
        <td> <button id='start'>start</button> <button id='stop'>stop</button></td>

    </tr>
    </table>
</body>
</html>

0 个答案:

没有答案