【问题标题】:Can't receive response with 503 error (Service Unavailable) in aws-sdk(node.js)无法在 aws-sdk(node.js)中收到 503 错误(服务不可用)的响应
【发布时间】:2015-05-10 03:56:24
【问题描述】:

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。 当我连接到我的网络服务器时,首先调用 Control 类。

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

【问题讨论】:

  • 我也有同样的问题=/

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


【解决方案1】:

我已经做到了。

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 文件中添加了我关注的一行,然后就完成了。

【讨论】:

    猜你喜欢
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 2020-02-12
    相关资源
    最近更新 更多