2016-01-22 74 views
0

我在使用Express託管應用服務器(版本:「body-parser」:1.14.2「,」express「:」^ 4.13.3「,」parse-http-標題「:」^ 1.0.0「,」x509「:」^ 0.2.3「) 節點v5.4.1 NPM v3.3.12Alexa JSON請求不符意圖模式

我已成功測試Alexa和我的本地應用服務器之間的SSL連接,但是,當我從Alexa的解析請求它不符合我的意圖模式服務模擬器下產生服務請求 - 測試

代碼:

const fs = require('fs'); //File System, for loading certifications and keys 
var http = require('http'); 
var https = require('https'); //Https server module 
var bodyParser = require('body-parser'); 
var parseHttpHeader = require('parse-http-header'); 

/***************** Credentials **********************/ 
const privateKey = fs.readFileSync('keys/private-key.pem'); //Private Key for validation (server uses only) 
const certificate = fs.readFileSync('certs/certificate.pem'); //Certificate, to provide to connecting host. 
const options = { 
    key: privateKey, 
    cert: certificate 
}; 

/******** Instantiate the Express server *************/ 
var express = require('express'); 
var app = express(); //Instantiate express server 

//Express configuration 
app.set('title', 'mySkillJS'); 
app.set('json spaces', 2); 

//Route all requests from app 
app.all('/*', function(request, response){ 
    //Handle request 
    var bucket = request; 
    console.log('\n \n \n &&&&&& \n \n \n'); 
    var joptions = { 
    type: 'application/json' 
    } 
    var bodyParsed = app.use(bodyParser.json(joptions)); 
    console.log(bodyParsed); 
    var headParsed = parseHttpHeader(bucket.headers['content-type'])[0]; 
    console.log(headParsed); 
}); 

//Ceate Server with SSL credentials 
var httpsServer = https.createServer(options, app).listen(8000);//createServer(options, fx(req, res)) 

意圖模式:

{ 
    "intents": [ 
    { 
     "intent": "HelloWorldIntent" 
    }, 
    { 
     "intent": "AMAZON.HelpIntent" 
    } 
    ] 
} 

產生的服務請求:從Alexa的

{ 
    "session": { 
    "sessionId": "SessionId.efb0235c-8201-4877-832e-58671b42c9b5", 
    "application": { 
     "applicationId": "amzn1.echo-sdk-ams.app.9eb766d1-f6a6-4ac7-9801-ce1478782b98" 
    }, 
    "user": { 
     "userId": "amzn1.echo-sdk-account.AHUTLGA6FMHPERGIUSQTTXG2RLOABOBTGSGN4LXBEWYK2XDLDRXU2" 
    }, 
    "new": true 
    }, 
    "request": { 
    "type": "IntentRequest", 
    "requestId": "EdwRequestId.734c7e4a-ec2c-4bdf-b2bc-37757796e670", 
    "timestamp": "2016-01-22T04:10:18Z", 
    "intent": { 
     "name": "HelloWorldIntent", 
     "slots": {} 
    } 
    } 
} 

請求由我的應用程序服務器處理:

&&&&&& 



{ [Function] 
    domain: undefined, 
    _events: { mount: [Function: onmount] }, 
    _maxListeners: undefined, 
    setMaxListeners: [Function: setMaxListeners], 
    getMaxListeners: [Function: getMaxListeners], 
    emit: [Function: emit], 
    addListener: [Function: addListener], 
    on: [Function: addListener], 
    once: [Function: once], 
    removeListener: [Function: removeListener], 
    removeAllListeners: [Function: removeAllListeners], 
    listeners: [Function: listeners], 
    listenerCount: [Function: listenerCount], 
    init: [Function: init], 
    defaultConfiguration: [Function: defaultConfiguration], 
    lazyrouter: [Function: lazyrouter], 
    handle: [Function: handle], 
    use: [Function: use], 
    route: [Function: route], 
    engine: [Function: engine], 
    param: [Function: param], 
    set: [Function: set], 
    path: [Function: path], 
    enabled: [Function: enabled], 
    disabled: [Function: disabled], 
    enable: [Function: enable], 
    disable: [Function: disable], 
    acl: [Function], 
    bind: [Function], 
    checkout: [Function], 
    connect: [Function], 
    copy: [Function], 
    delete: [Function], 
    get: [Function], 
    head: [Function], 
    link: [Function], 
    lock: [Function], 
    'm-search': [Function], 
    merge: [Function], 
    mkactivity: [Function], 
    mkcalendar: [Function], 
    mkcol: [Function], 
    move: [Function], 
    notify: [Function], 
    options: [Function], 
    patch: [Function], 
    post: [Function], 
    propfind: [Function], 
    proppatch: [Function], 
    purge: [Function], 
    put: [Function], 
    rebind: [Function], 
    report: [Function], 
    search: [Function], 
    subscribe: [Function], 
    trace: [Function], 
    unbind: [Function], 
    unlink: [Function], 
    unlock: [Function], 
    unsubscribe: [Function], 
    all: [Function: all], 
    del: [Function], 
    render: [Function: render], 
    listen: [Function: listen], 
    request: IncomingMessage { app: [Circular] }, 
    response: ServerResponse { app: [Circular] }, 
    cache: {}, 
    engines: {}, 
    settings: 
    { 'x-powered-by': true, 
    etag: 'weak', 
    'etag fn': [Function: wetag], 
    env: 'development', 
    'query parser': 'extended', 
    'query parser fn': [Function: parseExtendedQueryString], 
    'subdomain offset': 2, 
    'trust proxy': false, 
    'trust proxy fn': [Function: trustNone], 
    view: [Function: View], 
    views: '/home/dev/Alexa/alexa-js-mod/samples/helloWorld/src/views', 
    'jsonp callback name': 'callback', 
    title: 'mySkillJS', 
    'json spaces': 2 }, 
    _eventsCount: 1, 
    locals: 
    { settings: 
     { 'x-powered-by': true, 
     etag: 'weak', 
     'etag fn': [Function: wetag], 
     env: 'development', 
     'query parser': 'extended', 
     'query parser fn': [Function: parseExtendedQueryString], 
     'subdomain offset': 2, 
     'trust proxy': false, 
     'trust proxy fn': [Function: trustNone], 
     view: [Function: View], 
     views: '/home/dev/Alexa/alexa-js-mod/samples/helloWorld/src/views', 
     'jsonp callback name': 'callback', 
     title: 'mySkillJS', 
     'json spaces': 2 } }, 
    mountpath: '/', 
    _router: 
    { [Function: router] 
    params: {}, 
    _params: [], 
    caseSensitive: false, 
    mergeParams: undefined, 
    strict: false, 
    stack: [ [Object], [Object], [Object], [Object] ] } } 
application/json 

我不知道我在做什麼錯。我認爲請求將持有類似於我的意圖架構或生成的服務請求。爲什麼我沒有看到意圖架構或我生成的服務請求在JSON 請求我從Alexa收到的?

回答

0

所以這個問題是由於快遞模塊缺乏經驗而產生的。

我認爲bodyParsed會保存從app.use(bodyParser.json())返回的值;

當您使用app.use(bodyParser.json()); 它實際上將它存儲在請求的主體屬性中。

所以:

app.use(bodyParser.json(joptions));//causes request.body to populate with the JSON data 
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded requires multer 
app.all('/*', function(request, response){ 
    //Handle request 
    var bucket = request; 
    console.log('\n \n \n &&&&&& \n \n \n'); 
    var joptions = { 
    type: 'application/json' 
    } 
    console.log(request.body)); 
    var headParsed = parseHttpHeader(bucket.headers['content-type'])[0]; 
    console.log(headParsed); 
}); 

不知道這是否會強制所有請求將被解析爲application/JSON。也許移動app.all()中的bodyparser會導致它只在使用該路由時解析?