2017-12-18 53 views
0

我剛安裝moto,並試圖用下面的node.js代碼連接到獨立服務器:如何連接到moto獨立服務器?

const AWS = require('aws-sdk') 
const ep = new AWS.Endpoint('http://127.0.0.1:5000') 
const route53 = new AWS.Route53({endpoint: ep}) 

const params = {} 
console.log(route53.listHostedZones(params)) 

代碼設置AWS服務的endpoint,它指向的moto的獨立服務器。然後向端點發送listHostedZones請求。這個程序的輸出是

Request { 
domain: null, 
service: 
Service { 
    config: 
    Config { 
     credentials: [Object], 
     credentialProvider: [Object], 
     region: undefined, 
     logger: null, 
     apiVersions: {}, 
     apiVersion: null, 
     endpoint: [Object], 
     httpOptions: [Object], 
     maxRetries: undefined, 
     maxRedirects: 10, 
     paramValidation: true, 
     sslEnabled: true, 
     s3ForcePathStyle: false, 
     s3BucketEndpoint: false, 
     s3DisableBodySigning: true, 
     computeChecksums: true, 
     convertResponseTypes: true, 
     correctClockSkew: false, 
     customUserAgent: null, 
     dynamoDbCrc32: true, 
     systemClockOffset: 0, 
     signatureVersion: null, 
     signatureCache: true, 
     retryDelayOptions: {}, 
     useAccelerateEndpoint: false }, 
    endpoint: 
    Endpoint { 
     protocol: 'http:', 
     host: '127.0.0.1:5000', 
     port: 5000, 
     hostname: '127.0.0.1', 
     pathname: '/', 
     path: '/', 
     href: 'http://127.0.0.1:5000/', 
     constructor: [Object] }, 
    _clientId: 1 }, 
operation: 'listHostedZones', 
params: {}, 
httpRequest: 
HttpRequest { 
    method: 'POST', 
    path: '/', 
    headers: { 'User-Agent': 'aws-sdk-nodejs/2.166.0 darwin/v6.2.2' }, 
    body: '', 
    endpoint: 
    Endpoint { 
     protocol: 'http:', 
     host: '127.0.0.1:5000', 
     port: 5000, 
     hostname: '127.0.0.1', 
     pathname: '/', 
     path: '/', 
     href: 'http://127.0.0.1:5000/', 
     constructor: [Object] }, 
    region: undefined, 
    _userAgent: 'aws-sdk-nodejs/2.166.0 darwin/v6.2.2' }, 
startTime: 2017-12-18T13:27:48.148Z, 
response: 
Response { 
    request: [Circular], 
    data: null, 
    error: null, 
    retryCount: 0, 
    redirectCount: 0, 
    httpResponse: 
    HttpResponse { 
     statusCode: undefined, 
     headers: {}, 
     body: undefined, 
     streaming: false, 
     stream: null }, 
    maxRetries: 3, 
    maxRedirects: 10 }, 
_asm: 
AcceptorStateMachine { 
    currentState: 'validate', 
    states: 
    { validate: [Object], 
     build: [Object], 
     afterBuild: [Object], 
     sign: [Object], 
     retry: [Object], 
     afterRetry: [Object], 
     send: [Object], 
     validateResponse: [Object], 
     extractError: [Object], 
     extractData: [Object], 
     restart: [Object], 
     success: [Object], 
     error: [Object], 
     complete: [Object] } }, 
_haltHandlersOnError: false, 
_events: 
{ validate: 
    [ [Object], 
     [Function: VALIDATE_REGION], 
     [Function: BUILD_IDEMPOTENCY_TOKENS], 
     [Function: VALIDATE_PARAMETERS] ], 
    afterBuild: 
    [ [Object], 
     [Function: SET_CONTENT_LENGTH], 
     [Function: SET_HTTP_HOST] ], 
    restart: [ [Function: RESTART] ], 
    sign: [ [Object] ], 
    validateResponse: [ [Function: VALIDATE_RESPONSE] ], 
    send: [ [Object] ], 
    httpHeaders: [ [Function: HTTP_HEADERS] ], 
    httpData: [ [Function: HTTP_DATA] ], 
    httpDone: [ [Function: HTTP_DONE] ], 
    retry: 
    [ [Function: FINALIZE_ERROR], 
     [Function: INVALIDATE_CREDENTIALS], 
     [Function: EXPIRED_SIGNATURE], 
     [Function: CLOCK_SKEWED], 
     [Function: REDIRECT], 
     [Function: RETRY_CHECK] ], 
    afterRetry: [ [Object] ], 
    build: [ [Function: buildRequest], [Function: sanitizeUrl] ], 
    extractData: [ [Function: extractData], [Function: extractRequestId] ], 
    extractError: [ [Function: extractError], [Function: extractRequestId] ], 
    httpError: [ [Function: ENOTFOUND_ERROR] ] }, 
emit: [Function: emit] } 

正如你可以在上面的輸出中看到,有一個ENOTFOUND_ERROR錯誤的。我無法在moto的控制檯輸出中找到任何新的連接。

我開始摩托車的方式是moto_server route53

> moto_server route53 
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 

這有什麼錯我的配置?我認爲我的node.js代碼有問題。我是否誤解了endpoint的含義?

回答

0
const AWS = require('aws-sdk') 
const ep = new AWS.Endpoint('http://127.0.0.1:5000') 
const initParam = { 
    endpoint: ep, 
    region: 'ap-northeast-1' 
} 
const route53 = new AWS.Route53(initParam) 

let params = { 
    CallerReference: 'unique_string_affh38h98hasd8f76a', 
    Name: 'www.example.com' 
} 
route53.createHostedZone(params, (err, data) => { 
    if (err) { 
    console.error(`createHostedZone err: ${err}`) 
    } else { 
    console.log(`createHostedZone data: ${JSON.stringify(data)}`) 
    } 
}) 

我忘了設置region參數。我查看listHostedZones()的輸出的方式是錯誤的。糾正它們後,代碼正常工作。