node 小知识

福尔摩斯对我笑 2018-11-29 60610 次 8272条

electron 屏蔽错误,可捕捉进程错误

// //捕获错误
process.on('uncaughtException', function(err) {
console.log(err.stack);
console.log('NOT exit...');
});

###  FTP

同一FTP同一IP不能超过8个链接

### 递归执行,解决node并发数问题

"use strict"
const request = require('request')
const fs = require('fs')

const arr = [
    'http://path.to/img1.jpg',
    'http://path.to/img2.jpg',
    'http://path.to/img3.jpg',
    ...
    'http://path.to/img498.jpg',
    'http://path.to/img499.jpg',
    'http://path.to/img500.jpg'
]

const download = (arr)=>{
    src = arr.pop();
    //下载图片
    const ws = fs.createWriteStream('./download/'+src.split('/').pop());
    ws.on('close', ()=>{
        console.log('下载完成')
        if (arr.length>0)
            return download(arr);
    })
    request(src).pipe(ws);
}

封装基本的表单验证类 -》 formVaildate

const formVaildate = (arr)=>{
    for(let i = 0; i < arr.length; i++){
        //判空
        if(arr[i].required)
        {
            if(arr[i].data == null || arr[i].data == '')
            {                
                arr[i].required()
                return 0;
            }
        }
        //正则
        if( arr[i].regExp && arr[i].regExp.regVal.test(arr[i].data) === false )
        {
            arr[i].regExp.callback();
            return 0;
        }
    }


    return true;
}

module.exports =  formVaildate

使用方法:

const userInfo = vaildate([
        {
            data: gender,
            required: ()=>{
                resJson.code = 1;
                resJson.msg = '请填写性别';
                res.json(resJson);
            },
            regExp:{
                regVal: /\d/,
                callback: () => {
                    resJson.code = 1;
                    resJson.msg = '性别格式有误';
                    res.json(resJson);
                }
            }
        },
        {
            data: year,
            required: ()=>{
                resJson.code = 1;
                resJson.msg = '请填写年份';
                res.json(resJson);
            }
        }
])

nvm使用

下载安装: https://github.com/coreybutler/nvm-windows/releases

找到安装目录,打开settings.txt,将下面的配置写到文件


node_mirror: https://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/

// nvm list // 你本机中所有的node的版本列表

// nvm install latest // 安装最新版本

// nvm install 4.8.4 // 安装指定版本

// nvm use 10.8.0 // 当前使用版本

nvm arch [32|64]: 显示node是运行在32位还是64位模式。指定32或64来覆盖默认体系结构。

-nvm install <version> [arch]:该可以是node.js版本或最新稳定版本latest。(可选[arch])指定安装32位或64位版本(默认为系统arch)。设置[arch]为all以安装32和64位版本。在命令后面添加–insecure,可以绕过远端下载服务器的SSL验证。

nvm list [available]:列出已经安装的node.js版本。可选的available,显示可下载版本的部分列表。这个命令可以简写为nvm ls [available]。

nvm on: 启用node.js版本管理。

nvm off: 禁用node.js版本管理(不卸载任何东西)

nvm proxy [url]: 设置用于下载的代理。留[url]空白,以查看当前的代理。设置[url]为none删除代理。

nvm node_mirror [url]:设置node镜像,默认为https://nodejs.org/dist/.。可以设置为淘宝的镜像https://npm.taobao.org/mirrors/node/

nvm npm_mirror [url]:设置npm镜像,默认为https://github.com/npm/npm/archive/。可以设置为淘宝的镜像https://npm.taobao.org/mirrors/npm/

nvm uninstall <version>: 卸载指定版本的nodejs。

nvm use [version] [arch]: 切换到使用指定的nodejs版本。可以指定32/64位[arch]。

-nvm use <arch>:将继续使用所选版本,但根据提供的值切换到32/64位模式

nvm root [path]: 设置 nvm 存储node.js不同版本的目录 ,如果未设置,将使用当前目录。

-nvm version: 显示当前运行的nvm版本,可以简写为nvm v

Busyboy文件上传模块使用

若想在文件上传途中,做检测,并返回信息,必须进行文件释放

stream.resume();

递归读取文件大小



  function readFileList(path, filesList) {
    console.log(path);
    var files = fs.readdirSync(path);
    files.forEach(function (itm, index) {
        var stat = fs.statSync(path + "/"+ itm);
        if (stat.isDirectory()) {
        //递归读取文件
            readFileList(path + "/" + itm , filesList)
        } else {
            var {size} =  fs.statSync(path + "/"+ itm);
            filesList.push(size);
        }


    })
}
标签: 无

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 8272 条评论 1


  1. Furniture installers

    There are some interesting points in time in this article but I don’t know if I see all of them center to heart. There may be some validity but I will take hold opinion till I look into it further. Good article , thanks and we want extra! Added to FeedBurner as properly

    回复 2019年12月8日
  2. Furniture assembly help

    Hi are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and set up my own. Do you need any coding expertise to make your own blog? Any help would be really appreciated!

    回复 2019年12月8日
  3. UFABET

    I’m impressed, I have to admit. Seldom do I come across a blog that’s equally educative and engaging, and without a doubt, you have hit the nail on the head. The issue is something too few people are speaking intelligently about. I am very happy that I came across this during my hunt for something regarding this.

    回复 2019年12月7日
  4. ultimate installation

    Normally I don’t read article on blogs, but I wish to say that this write-up very forced me to check out and do so! Your writing taste has been surprised me. Thanks, quite great article.

    回复 2019年12月7日
  5. Delta installation

    Real fantastic information can be found on web site. “I can think of nothing less pleasurable than a life devoted to pleasure.” by John D. Rockefeller.

    回复 2019年12月7日
  6. 스포츠티비

    Thanks for the sensible critique. Me and my neighbor were just preparing to do a little research on this. We got a grab a book from our area library but I think I learned more clear from this post. I’m very glad to see such great information being shared freely out there.

    回复 2019年12月6日
  7. AMI installation

    You have observed very interesting points ! ps decent website . “To grow mature is to separate more distinctly, to connect more closely.” by Hugo Von Hofmannsthal.

    回复 2019年12月6日
  8. udaipur escorts

    Excellent read, I just passed this onto a friend who was doing a little research on that. And he actually bought me lunch as I found it for him smile Thus let me rephrase that: Thank you for lunch! “Without friends no one would choose to live, though he had all other goods.” by Aristotle.

    回复 2019年12月6日
  9. togel hk

    I was examining some of your blog posts on this internet site and I think this website is very informative ! Retain putting up.

    回复 2019年12月6日
  10. graphic design

    great points altogether, you just won a new reader. What might you recommend about your put up that you made some days in the past? Any positive?

    回复 2019年12月5日
  11. Encontros de Sexo

    You ought to be a part of a contest for one of the best websites on the web. I most certainly will recommend this blog!

    回复 2019年12月5日
  12. semsi

    I must thank you for the efforts you’ve put in penning this blog. I’m hoping to see the same high-grade content from you later on as well. In fact, your creative writing abilities has inspired me to get my own blog now 😉

    回复 2019年12月4日
  13. i99bet

    Good information. Lucky me I recently found your site by accident (stumbleupon). I’ve saved it for later!

    回复 2019年12月4日
  14. i99bet

    Good post. I learn something new and challenging on websites I stumbleupon everyday. It’s always useful to read articles from other authors and practice something from other websites.

    回复 2019年12月3日
  15. John Deere Technical Manuals

    You produced some decent points there. I looked on the net to the issue and discovered most people goes together with along with your website.

    回复 2019年12月2日
  16. Celsa Sulivan

    It’s nearly impossible to find experienced people in this particular topic, but you seem like you know what you’re talking about! Thanks

    回复 2019年12月2日
  17. Clay Zimmerman

    This website certainly has all of the information and facts I needed about this subject and didn’t know who to ask.

    回复 2019年12月2日
  18. Tworzenie Stron Www Gorzów Wlkp

    You have brought up a very good details , thanks for the post.

    回复 2019年11月30日
  19. Hector Mertz

    I will be brand-new to blogging and also loved your internet site. I will bookmark your blog and keep checking get you started. Thanks for sharing your internet site.

    回复 2019年11月28日
  20. Sylvie Rettkowski

    I do not even understand how I stopped up here, however I thought this submit was once good. I do not realize who you’re but certainly you are going to a well-known blogger for those who aren’t already Cheers!

    回复 2019年11月28日
  21. view publisher site

    I just want to say I’m newbie to blogging and site-building and actually savored you’re web-site. Probably I’m want to bookmark your site . You actually come with great article content. Cheers for revealing your website page.

    回复 2019年11月24日
  22. StevDede

    Viagra Para El Deporte cialis tablets for sale Acheter Du Cytotec En Ligne Sans Ordonnance Doxycycline Acne Xenical No Prescription

    回复 2019年11月22日