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

时间:2015-03-09 02:38:29

标签: node.js amazon-ec2 response http-status-code-503 aws-sdk

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

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

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);
            }
        });
    }
}

我编程如上,节点web服务器运行良好。 awsControl.js是服务器端的javascript,control.js是客户端的javascript。 当我连接到我的Web服务器时,首先调用Control类。

这是麻烦。当我使用startInstance(AWS-SDK API)发送请求时,它在服务器端工作。 但是,我无法收到503错误(服务不可用)的响应。 在客户端,我总是收到错误回调503错误。 我不知道为什么我无法收到回复。 我设置安全组(EC2)和NACL(VPC),所以我不认为它的防火墙有问题。 有没有人可以告诉我如何找到解决方案?

1 个答案:

答案 0 :(得分:0)

我做到了。

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
        }  
    });

我只添加了一行我关注awsControl.js文件,然后完成。