美文网首页
如何调用其他服务器的接口

如何调用其他服务器的接口

作者: 散樱乱舞 | 来源:发表于2019-10-21 19:46 被阅读0次
//向其他服务器发起请求
const http = require('http');

//http请求壳子
function request_mi(opt, data) {
    return new Promise((resolve, reject) => {
        http.request(opt, (res) => {
            res.setEncoding('utf8');
            let body = '';
            res.on("data", (chunk) => {
                body += chunk;
            });
            res.on("end", () => {
                resolve(JSON.parse(body));
            });
        }).on("error", (err) => {
            reject(err.message);
        }).end(data);
    })
}

router.post('/logic', async function (req, res, next) {
    let data = {};
    data = JSON.stringify(data);
    let opt = {
        host: '127.0.0.1',
        port: '7788',
        method: 'GET',
        path: '/api/file',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': data.length
        }
    }

    let results = await request_mi(opt, data);
    res.json(results);
});

相关文章

网友评论

      本文标题:如何调用其他服务器的接口

      本文链接:https://www.haomeiwen.com/subject/ijeamctx.html