分类 NodeJS 的文章 累计 12 条

dhso dhso 统计中 2018-06-27 16:58

Nodejs下载文件

function downloadFile(_url, _filename, _rewrite, _startFunc, _endFunc, _errorFunc) {
  if (!_filename) {
    let us = _url.split('/');
    _filename = us[us.length - 1];
  }
  _mkdirsSync(path.join(app.getPath("userData"), 'resources'));
  let _realpath = path.join(app.getPath("userData"), 'resources', _filename);
  if (_rewrite) {
    _download(_url, _realpath);
    return;
  }
  fs.exists(_realpath, (exists) => {
    if (!exists) _download(_url, _realpath);
  })

  function _downloa
[ 阅读更多 ]
dhso dhso 统计中 2018-06-27 16:57

fs递归删除文件或者文件夹

function deleteRecursive(path) {
  if (fs.existsSync(path)) {
    //  file
    if (fs.statSync(path).isFile()) {
      fs.unlinkSync(path);
      return;
    }
    // directory
    let files = [];
    if (fs.statSync(path).isDirectory()) {
      files = fs.readdirSync(path);
      files.forEach(function (file, index) {
        deleteRecursive(path + "/" + file);
      });
      fs.rmdirSync(path);
    }
  }
};
[ 阅读更多 ]
dhso dhso 统计中 2018-05-04 02:00

nodejs动态加载文件夹内模块

定义 ./modules/index.js

const fs = require("fs");
const colors = require("colors");
module.exports = {
    controller: null,
    path: '',
    init: function(path, controller) {
        if (!controller) {
            console.error("参数controller未设置");
            return false;
        }
        this.controller = controller;
        this.path = path ? path : this.path;
        this.listDir(this.path);
    },
    listDir: function(dir) {
        var fileList = fs.readdirSync(dir, 'u
[ 阅读更多 ]
dhso dhso 统计中 2018-05-03 22:11

nodejs控制台彩色文字输出

console.log('\x1B[36m%s\x1B[0m', info);  //cyan  
console.log('\x1B[33m%s\x1b[0m:', path);  //yellow  
var styles = {  
    'bold'          : ['\x1B[1m',  '\x1B[22m'],  
    'italic'        : ['\x1B[3m',  '\x1B[23m'],  
    'underline'     : ['\x1B[4m',  '\x1B[24m'],  
    'inverse'       : ['\x1B[7m',  '\x1B[27m'],  
    'strikethrough' : ['\x1B[9m',  '\x1B[29m'],  
    'white'         : ['\x1B[37m', '\x1B[39m'],  
    'grey'          : ['\x1B[90m', '\x1B[39m'],  
    'black'         : ['\x1B[30m', '\x1B[3
[ 阅读更多 ]
dhso dhso 统计中 2017-11-21 11:49

axios异步转同步请求模式

axios是一个优秀的http异步请求库,但是很多时候需要做一些同步请求,于是我封装了下同步的方法

axiosPro 函数代码

const axios = require('axios')

function axiosPro(axiosArgs) {
  const { method, url, data, params, headers, responseType } = axiosArgs
  return new Promise(function (resolve, reject) {
    axios({
      method: method,
      url: url,
      data: data,
      params: params,
      headers: headers,
      responseType: responseType
    }).then(function (ret) {
      resolve(ret);
    }).catch(function (err) {
      reject(err);
    })
  })
};
[ 阅读更多 ]
博客介绍
写好程序在于点滴积累,多做笔记!
热评文章
最新评论