defineconst
var https = require(\'https\')
var express = require(\'express\');
var app = express();
var bodyParser = require("body-parser");
var request = require(\'request\')
var fs = require(\'fs\'); // 引入fs模块

//访问端口
var port = 9999

//接受post数据
app.use(bodyParser.json({
    "limit": "100mb"
}));
app.use(bodyParser.urlencoded({
    limit: \'50mb\',
    extended: true
}));

//设置跨域访问
app.all(\'*\', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", \' 3.2.1\')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
var qs = require(\'querystring\');

const param = qs.stringify({
    \'grant_type\': \'client_credentials\',
    \'client_id\': \'XXXXX\',
    \'client_secret\': \'XXXXXX\'
});


function log(data)
{
    fs.writeFile(\'imgdata.txt\', data, {
        \'flag\': \'a\'
    }, function (err) {
        if (err) {
            throw err;
        }

        console.log(\'Hello.\');

    });
}

app.post(\'/XXXXDetectV1\', function (req, res) {

    var imgdata = req.body.imgdata; //body query
    // console.log(imgdata) 
    imgdata = imgdata.replace("data:image/png;base64,","")
    log(imgdata)
    https.get({
            hostname: \'aip.baidubce.com\',
            path: \'/oauth/2.0/token?\' + param,
            agent: false
        },
        function (res2) {
            // 在标准输出中查看运行结果
            // res2.pipe(process.stdout);
            // console.log(\'statusCode:\', res2.statusCode);
            // console.log(\'headers:\', res2.headers);

            res2.on(\'data\', (d) => {
                console.log("认证成功")
                // console.log(\'\' + d); //将buffer转为字符串或者使用d.toString()
                let b = JSON.parse(\'\' + d); //将buffer转成JSON
                // console.log(b)
                var access_token = b["access_token"]
                console.log(access_token)
                // res.end(JSON.stringify(b));
                data = {
                    \'image\': imgdata
                }
     
                request_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/detection/XXXXDetectV1" + "?access_token=" + access_token
 
                request({
                    url: request_url,
                    method: "POST",
                    json: true,
                    headers: {
                        "content-type": "application/json",
                    },
                    body: data
                }, function (error, response, body) {
                    console.log(error)
                    // console.log(response)
                    // console.log(body)
                    if (!error && response.statusCode == 200) {
                        console.log(body) // 请求成功的处理逻辑
                        res.end(JSON.stringify(body));
                    }
                });


            });

        }
    );

})


//启动服务---------------------------------------------------------------------------------------------
var server = app.listen(port, function () {

    var host = server.address().address
    var port = server.address().port

    console.log("应用实例,访问地址为 http://%s:%s", host, port)

})

 

分类:

技术点:

相关文章:

  • 2021-11-09
  • 2021-12-03
  • 2021-09-05
  • 2021-10-26
  • 2021-11-11
  • 2021-12-31
猜你喜欢
  • 2021-06-13
  • 2021-10-16
  • 2021-10-16
  • 2021-11-01
  • 2021-10-16
  • 2021-10-16
  • 2021-11-01
相关资源
相似解决方案