主要解决的问题的,以最近VsCode插件开发为例,每次请求都需要token,而vscode并不支持cookie这样的存储,所以就采用粗暴点办法,存到某个用户目录下并读取。

源码如下:

var fs=require("fs");

 function getToken(isRelease) {
        //是否为正式版本,路径不一样
        if (isRelease) {
            const scriptSrc = path.dirname(__filename);
            const jsName = scriptSrc.split('\\');
            var i = jsName.length;
            var finpath = "";
            for (var j = 0; j < i - 3; j++) {
                if (j == 0) {
                    finpath = jsName[j];
                } else {
                    finpath = finpath + '\\' + jsName[j];
                }
            }
            finpath = finpath + '\\token.txt';

        } else { 
            finpath = 'D://Workspace//token//token.txt';
        }


        if (fs.existsSync(finpath)) { //判断是否存在该文件
            try {
                let result = fs.readFileSync(finpath);
                console.log(result.toString());



                return result.toString();
            
            } catch (e) {

            }

        }


    }
    
    console.log(getToken());

 

相关文章:

  • 2021-12-03
  • 2022-12-23
  • 2022-02-25
  • 2021-08-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-16
相关资源
相似解决方案