Skip to content

Latest commit

 

History

History
75 lines (56 loc) · 1.95 KB

File metadata and controls

75 lines (56 loc) · 1.95 KB

Quickstart: Project Answer Search with Node

The following Node example creates a query for information about Yosemite National Park.

Prerequisites

Get an access key for the free trial Cognitive Research Technologies

This example uses Node v8.9.4

Code scenario

The following code gets answers. It is implemented in the following steps:

  1. Declare variables to specify the endpoint by host and path.
  2. Specify the query URL to preview, and add the query parameter.
  3. Create a handler function for the response.
  4. Define the Search function that creates the request and adds the Ocp-Apim-Subscription-Key header.
  5. Run the Search function.

The complete code for this demo follows:

'use strict';

let https = require('https');

// Replace the subscriptionKey string value with your valid subscription key.
let subscriptionKey = 'YOUR-SUBSCRIPTION-KEY'; 

let host = 'api.labs.cognitive.microsoft.com';
let path = '/answerSearch/v7.0/search';

let mkt = 'en-us';
let q = 'Yosemite National Park';

let params = '?q=' + encodeURI(q) + '&mkt= + mkt;

let response_handler = function (response) {
    let body = '';
    response.on('data', function (d) {
        body += d;
    });
    response.on('end', function () {
        let body_ = JSON.parse(body);
        let body__ = JSON.stringify(body_, null, '  ');
        console.log(body__);
    });
    response.on('error', function (e) {
        console.log('Error: ' + e.message);
    });
};

let Search = function () {
    let request_params = {
        method: 'GET',
        hostname: host,
        path: path + params,
        headers: {
            'Ocp-Apim-Subscription-Key': subscriptionKey,
        }
    };

    let req = https.request(request_params, response_handler);
    req.end();
}

Search();

Next steps