简体   繁体   English

在aws-sdk(node.js)中无法接收到503错误(服务不可用)的响应

[英]Can't receive response with 503 error (Service Unavailable) in aws-sdk(node.js)

app.js app.js

var express = require('express');
var express_namespace = require('express-namespace');
var path = require('path');
var favicon = require('serve-favicon');
var http = require('http');
var https = require('https');
var e_logger = require('morgan');
var logger = require('./logger.js').getLogger('framework');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var awsControl = require('./routes/awsControl')

var aws = require('aws-sdk');
var ec2 = new aws.EC2();

var app = express();
var server = http.createServer(app);

var env = process.env.NODE_ENV || 'development';

if ('development' == env) {

    app.set('port', 80);

    app.use(express.static(path.join(__dirname, 'public')));

    app.set('views', path.join(__dirname, 'views'));
    app.engine('html', require('ejs').renderFile);
    app.set('view engine', 'html');

    app.use(cookieParser());
    app.use(e_logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(require('stylus').middleware(path.join(__dirname, 'public')));

    app.use('/', routes);
}

// routes
app.namespace('/', function () {
    app.get('describeInstances', awsControl.describeInstances);

});

server.listen(app.get('port'), function () {});

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404
    logger.error(err);
    next(err);
});

awsControl.js awsControl.js

var aws = require('aws-sdk'); 
var util = require('util');

aws.config.update({
    accessKeyId: "myKey",
    secretAccessKey: "mySecretKey",
    region: "ap-northeast-1"
});
console.log("config = " + util.inspect(aws.config));

var ec2 = new aws.EC2({ region: "ap-northeast-1" });

var app01 = 'aaaaaaaa';
var DB01 = 'bbbbbbbb';


exports.describeInstances = function (req, res) {
    var params = {
        Filters: [{
                Name: 'vpc-id',
                Values: ['vpc-cccccccc']
            }],
        DryRun: false
    };

    ec2.describeInstances(params, function (err, data) {
        if (err) { // an error occurred
            console.log(err, err.stack);
        } else { // successful response
            console.log(data);
        }  
    });
}

control.js control.js

var Control = function () {
    this.initializePage();
};

Control.prototype = new Object();

Control.prototype = {
    initializePage : function () {
        $.ajax({
            type: "GET",
            url: "describeInstances", 
            success : function (data, status, jQxhr) {

                console.log("### describeInstances success");
                console.log(data);

            },
            error : function (jQxhr, status, error) {
                console.log("### describeInstances error");
                console.log(error);
                console.log(jQxhr);
            },
            complete : function (jQxhr, status) {
                console.log("### describeInstances complete");
                console.log(jQxhr);
            }
        });
    }
}

I programmed like above and the node web server is operating well. 我像上面一样编程,节点Web服务器运行良好。 awsControl.js is server-side javascript and control.js is client-side javascript. awsControl.js是服务器端javascript,而control.js是客户端javascript。 When I connect to my web server, Control class is called first. 当我连接到Web服务器时,将首先调用Control类。

Here is the trouble. 这是麻烦。 When I send request with startInstance (AWS-SDK API), it's working in server-side. 当我使用startInstance(AWS-SDK API)发送请求时,它在服务器端工作。 However, I can't receive response with 503 error(service unavailable). 但是,我无法收到503错误(服务不可用)的响应。 On client-side, I always receive error callback with 503 error. 在客户端,我总是收到带有503错误的错误回调。 I don't know why I can't receive response. 我不知道为什么我无法收到回复。 I set security groups(EC2) and NACL(VPC) up so I don't think that it's firewall trouble. 我设置了安全组(EC2)和NACL(VPC),所以我不认为这是防火墙的问题。 Is there anybody can tell me how I can find the solution out? 有谁可以告诉我如何找到解决方案?

I have done this. 我已经做到了

ec2.describeInstances(params, function (err, data) {
        if (err) { // an error occurred
            console.log(err, err.stack);
        } else { // successful response
            console.log(data);
            res.send(data); <-- this line
        }  
    });

I added just a line I focus to awsControl.js file, then done. 我只在awsControl.js文件中添加了一行,然后完成了。

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

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