简体   繁体   English

使用nodeJS的AWS请求

[英]AWS requests using nodeJS

I have a webpage which has a button. 我有一个带有按钮的网页。 On clicking of button it calls a nodejs method which returns the status of my EC2 instances on my AWS account. 单击按钮后,它将调用nodejs方法,该方法返回我的AWS账户上我的EC2实例的状态。 To host webpage I have used nodeJS. 为了托管网页,我使用了nodeJS。 For connection between my aws account and webpage I have used AWS NodeJS sdk. 为了在AWS账户和网页之间建立连接,我使用了AWS NodeJS sdk。

The response I am getting (statuses of EC2) is in json format.Following is the response- 我得到的响应(EC2的状态)为json格式。以下是响应-

{ InstanceStatuses:
   [ { InstanceId: 'i-67441a9c',
       AvailabilityZone: 'us-east-1c',
       Events: [],
       InstanceState: [Object],
       SystemStatus: [Object],
       InstanceStatus: [Object] },
     { InstanceId: 'i-feac4e0f',
       AvailabilityZone: 'us-east-1b',
       Events: [],
       InstanceState: [Object],
       SystemStatus: [Object],
       InstanceStatus: [Object] } ] }

C:\NodeTest>node DescribeInstances.js
{ InstanceStatuses:
   [ { InstanceId: 'i-67441a9c',
       AvailabilityZone: 'us-east-1c',
       Events: [],
       InstanceState: [Object],
       SystemStatus: [Object],
       InstanceStatus: [Object] },
     { InstanceId: 'i-feac4e0f',
       AvailabilityZone: 'us-east-1b',
       Events: [],
       InstanceState: [Object],
       SystemStatus: [Object],
       InstanceStatus: [Object] } ] }

This response is coming in command prompt. 此响应将出现在命令提示符下。 Now I want this response to be displayed on a webpage. 现在,我希望此响应显示在网页上。 How should I do it. 我应该怎么做。 Or how can I use response of ec2 instance to take further action like if one ec2 is running the close it. 或者,如何使用ec2实例的响应来采取进一步的操作,例如是否正在运行一个ec2来关闭它。 Any help would be appreciated. 任何帮助,将不胜感激。 I am new to NodeJs. 我是NodeJ的新手。 Thanks in Advance. 提前致谢。

What you have posted here is "backend code", or code that runs behind your server that your users will never see. 您在此处发布的是“后端代码”,即在您的服务器后面运行的代码,您的用户永远不会看到。

You need to add "front end code" to this system. 您需要向该系统添加“前端代码”。 Android apps, windows applications, and websites are all examples of "front ends". Android应用程序,Windows应用程序和网站都是“前端”的示例。

Common nodejs libraries used to provide a web front end are: 用于提供Web前端的常见nodejs库是:

For the rest of this answer, I'll be assuming you choose express because it's the easiest for a beginner to understand, in my opinion. 对于本答案的其余部分,我假设您选择表达,因为我认为这是初学者最容易理解的。

A simple webserver 一个简单的网络服务器

The simplest example of adding a webpage would look like this. 添加网页的最简单示例如下所示。

var express = require( 'express' )
var app = express()
var myAwsFetcher = require( './aws-fetcher' )

app.get( '/', function( req, res ){
  myAwsFetcher( function( err, data ){
    res.send( data )
  })
})

app.listen( 8888, function(){
  console.log( 'App is listening on localhost:8888' )
})

This way, when you run node index.js and go to http://localhost:8888 , you should see in your browser: 这样,当您运行node index.js并转到http://localhost:8888 ,您应该在浏览器中看到:

{ InstanceStatuses:
   [ { InstanceId: 'i-67441a9c',
       AvailabilityZone: 'us-east-1c',
       Events: [],
       InstanceState: [Object],
       SystemStatus: [Object],
       InstanceStatus: [Object] },
     { InstanceId: 'i-feac4e0f',
       AvailabilityZone: 'us-east-1b',
       Events: [],
       InstanceState: [Object],
       SystemStatus: [Object],
       InstanceStatus: [Object] } ] }

To display the response you can define: 要显示响应,您可以定义:

<body>
<textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
</body>

and replace: console.log(response); 并替换为: console.log(response); whith: document.getElementById('textarea').innerHTML = response; 蒙山: document.getElementById('textarea').innerHTML = response;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM