【问题标题】:Connect Microsoft Azure Machine Learning Studio API using Vue Axios or using Express Server?使用 Vue Axios 还是使用 Express Server 连接 Microsoft Azure Machine Learning Studio API?
【发布时间】:2019-12-30 18:27:41
【问题描述】:

目前我正在使用以下代码连接网络服务。 我需要使用 Vue Axios 或 Express 连接到 Microsoft Azure Machine Learning Studio Api。谁能帮帮我。

var http = require("http");
var https = require("https");
var querystring = require("querystring");
var fs = require('fs');

function getPred(data) {
    console.log('===getPred()===');
    var dataString = JSON.stringify(data)
    var host = 'ussouthcentral.services.azureml.net'
    var path = '/workspaces/fda91d2e52b74ee2ae68b1aac4dba8b9/services/1b2f5e6f99574756a8fde751def19a0a/execute?api-version=2.0&details=true'
    var method = 'POST'
    var api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=='
    var headers = {'Content-Type':'application/json', 'Authorization':'Bearer ' + api_key};

    var options = {
        host: host,
        port: 443,
        path: path,
        method: 'POST',
        headers: headers
    };

    console.log('data: ' + data);
    console.log('method: ' + method);
    console.log('api_key: ' + api_key);
    console.log('headers: ' + headers);
    console.log('options: ' + options);

    var reqPost = https.request(options, function (res) {
        console.log('===reqPost()===');
        console.log('StatusCode: ', res.statusCode);
        console.log('headers: ', res.headers);

        res.on('data', function(d) {
            process.stdout.write(d);
        });
    });

    // Would need more parsing out of prediction from the result
    reqPost.write(dataString);
    reqPost.end();
    reqPost.on('error', function(e){
        console.error(e);
    });
}

//Could build feature inputs from web form or RDMS. This is the new data that needs to be passed to the web service.
function buildFeatureInput(){
    console.log('===performRequest()===');
    var data = {
        "Inputs": {
            "input1": {
                "ColumnNames": ["gl10", "roc20", "uo", "ppo", "ppos", "macd", "macds", "sstok", "sstod", "pmo", "pmos", "wmpr"],
                "Values": [ [ "0", "-1.3351", "50.2268", "-0.2693", "-0.2831", "-5.5310", "-5.8120", "61.9220", "45.3998", "-0.0653", "-0.0659", "-30.3005" ], ]
            },
        },
        "GlobalParameters": {}
    }
    getPred(data);
}

function send404Reponse(response) {
    response.writeHead(404, {"Context-Type": "text/plain"});
    response.write("Error 404: Page not Found!");
    response.end();
}

function onRequest(request, response) {
    if(request.method == 'GET' && request.url == '/' ){
        response.writeHead(200, {"Context-Type": "text/plain"});
        fs.createReadStream("./index.html").pipe(response);
    }else {
        send404Reponse(response);
    }
}

http.createServer(onRequest).listen(8050);
console.log("Server is now running on port 8050");
buildFeatureInput();

但是我可以通过使用 axios 调用或 express 服务器来做到这一点吗?

如果我可以使用 vue axios 或 express server 执行此操作,任何人都可以帮助我使用正确的语法。

【问题讨论】:

    标签: javascript node.js azure express vuetify.js


    【解决方案1】:

    听起来您想在服务器端使用 express 和 Vue 首页中的 axios,而不是在服务器端使用 Node http 服务器和 https 客户端。

    express替换节点http很简单,如下。

    const express = require('express')
    const path = require('path');
    const app = express()
    const port = 8050
    
    app.use(express.static(path.join(__dirname, '.')))
    app.get('/', (req, res) => res.sendFile('index.html'))
    
    app.use(function (req, res, next) {
        var err = new Error('Not Found');
        err.status = 404;
        next(err)
    })
    
    app.use(function (err, req, res, next) {
        if(err.status == 404) {
            res.status(404).send("Error 404: Page not Found!")
        }
        res.status(500).send("Error 500: Internal Error!")
    })
    
    app.listen(port, () => console.log("Server is now running on port 8050"))
    

    但是考虑到调用 Azure Machine Learning Studio API 时 api-key 值的安全性,我建议不要在 Vue 首页中使用 axios 调用 API,并且仍然通过 @ 在服务器端进行调用987654330@,如下。

    const axios = require('axios');
    
    var host = 'ussouthcentral.services.azureml.net'
    var path = '/workspaces/fda91d2e52b74ee2ae68b1aac4dba8b9/services/1b2f5e6f99574756a8fde751def19a0a/execute?api-version=2.0&details=true'
    var api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=='
    
    const pred = axios.create({
      baseURL: 'https://'+host,
      timeout: 1000,
      headers: {'Content-Type':'application/json', 'Authorization':'Bearer ' + api_key}
    });
    
    app.post('/mls-api', (req, res) => pred.post(path, JSON.stringify(req.body)).then(function(resp) {
        resp.pipe(res)
        }))
    

    然后,您可以从 Vue 首页调用/mls-api url,并使用下面的data 值。

    var data = {
        "Inputs": {
            "input1": {
                "ColumnNames": ["gl10", "roc20", "uo", "ppo", "ppos", "macd", "macds", "sstok", "sstod", "pmo", "pmos", "wmpr"],
                "Values": [ [ "0", "-1.3351", "50.2268", "-0.2693", "-0.2831", "-5.5310", "-5.8120", "61.9220", "45.3998", "-0.0653", "-0.0659", "-30.3005" ], ]
            },
        },
        "GlobalParameters": {}
    }
    
    axios.post('/mls-api', data)
      .then(function (response) {
        console.log(response);
      })
    

    【讨论】:

      猜你喜欢
      • 2015-02-19
      • 2020-04-28
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      相关资源
      最近更新 更多