I
I
Igor2020-02-02 12:57:26
Vue.js
Igor, 2020-02-02 12:57:26

Nodejs native in vue, how to make https a plugin?

Plan to use native Nodejs requests in Vue

Stack:
electron-vue
vue

require("follow-redirects").https

var https = require('follow-redirects').https;
var fs = require('fs');

var options = {
  'method': 'GET',
  'hostname': 'example.com',
  'path': 'example.com',
  'headers': {
  },
  'maxRedirects': 20
};

var req = https.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function (chunk) {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });

  res.on("error", function (error) {
    console.error(error);
  });
});

req.end();


Tried to do so

"use strict"

import Vue from "vue"

const _https = require("follow-redirects").https;

Plugin.install = function (Vue) {
    Vue.https = _https
    window.https = _https
    Object.defineProperties(Vue.prototype, {
        https: {
            get () {
                return _https
            }
        },
        $https: {
            get () {
                return _https
            }
        }
    })
}

Vue.use(https)

export default Plugin


I don't see the plugin in the main context.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2020-02-02
@IgorPI

The problem is a name conflict.
Here is a working version.
Easier nowhere.

const { http, https } = require("follow-redirects")

const HttpsPlugin = {
  install:  function(Vue, options) {
    Vue.https = https
    window.https = https
    Object.defineProperties(Vue.prototype, {
      https: {
        get() {
          return https
        }
      },
      $https: {
        get() {
          return https
        }
      },
    })
  }
}

Vue.use(HttpsPlugin)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question