经常使用npm模块分享

日常平凡本身用的npm模块也不算少了,实在网上有许多牛人开辟的npm模块都很好,愿望不要被埋没了。
《经常使用npm模块分享》

一、 有用的模块

1.thunder-vip

作用:猎取最新可用的迅雷 vip 账号。
处理什么:不必每次翻开网站去找号。。
用法: $ thunder or

  var thunderVip = require('thunder-vip');

  thunderVip(function (err, accounts) {
  console.log(accounts);
  });

截图

《经常使用npm模块分享》

2. npm-user-downloads

npm-user-downloads

检察 npm 用户某个时间段内一切模块的下载量,按从高到低排名。

处理什么:起初是想看本身的那些模块下载量多,好重点保护。。

用法: $ nud hupengbest last-month --limit=20

截图:

《经常使用npm模块分享》

二、koa开辟模块

1. co

作用:异步掌握

1 栗子

    co(function* () {
      var result = yield Promise.resolve(true);
      return result;
    }).then(function (value) {
      console.log(value);
    }, function (err) {
      console.error(err.stack);
    });

2 假如你想把一个 co-generator-function 转成实在的functionu并返回一个promise 能够运用co.wrap(fn*)

    var fn = co.wrap(function* (val) {
      return yield Promise.resolve(val);
    });

    fn(true).then(function (val) {

    });

3 完全的example

    var co = require('co');

    co(function *(){
      // yield any promise
      var result = yield Promise.resolve(true);
    }).catch(onerror);

    co(function *(){
      // resolve multiple promises in parallel
      var a = Promise.resolve(1);
      var b = Promise.resolve(2);
      var c = Promise.resolve(3);
      var res = yield [a, b, c];
      console.log(res);
      // => [1, 2, 3]
    }).catch(onerror);

    // errors can be try/catched
    co(function *(){
      try {
        yield Promise.reject(new Error('boom'));
      } catch (err) {
        console.error(err.message); // "boom"
     }
    }).catch(onerror);

    function onerror(err) {
      // log any uncaught errors
      // co will not throw any errors you do not handle!!!
      // HANDLE ALL YOUR ERRORS!!!
      console.error(err.stack);
    }

4 api

co(fn*).then( val => )
处理一个generator然后返回一个promise

    co(function* () {
      return yield Promise.resolve(true);
    }).then(function (val) {
      console.log(val);
    }, function (err) {
      console.error(err.stack);
    });

var fn = co.wrap(fn*)
将一个generator转成一般的function并返回一个promise

    var fn = co.wrap(function* (val) {
      return yield Promise.resolve(val);
    });

    fn(true).then(function (val) {

    });

2、debug

npm install debug

  1. 运用方法

  //Example app.js
    var debug = require('debug')('http')
      , http = require('http')
      , name = 'My App';

    // fake app

    debug('booting %s', name);

    http.createServer(function(req, res){
      debug(req.method + ' ' + req.url);
      res.end('hello\n');
    }).listen(3000, function(){
      debug('listening');
    });

    // fake worker of some kind

    require('./worker');
 //Example worker.js:
    var debug = require('debug')('worker');

    setInterval(function(){
      debug('doing some work');
    }, 1000);

效果图
《经常使用npm模块分享》

在windows环境下须要设置环境变量set DEBUG=*,-not_this 我这里运用的是idea的debug调试
《经常使用npm模块分享》

windows 下启动体式格局
《经常使用npm模块分享》

将debug日记转存到文件中
DEBUG_FD=3 node your-app.js 3> whatever.log

3、koa-bodyparser

运用方法

  var koa = require('koa');
  var bodyParser = require('koa-bodyparser');

  var app = koa();
  app.use(bodyParser());

  app.use(function *() {
    this.body = this.request.body;
  });

在koa2中运用

npm install koa-bodyparser@next --save

4. koa-json

npm install koa-json --save

运用方法

var json = require('koa-json');
var Koa = require('koa');
var app = new Koa();

app.use(json());

app.use((ctx) => {
  ctx.body = { foo: 'bar' };
});

4. koa-webpack-dev-middleware

运用方法

npm install --save-dev koa-webpack-dev-middleware

var app = require('koa')();
var webpackMiddleware = require("koa-webpack-dev-middleware");
app.use(webpackMiddleware(webpack({
  // webpack options
  // webpackMiddleware takes a Compiler object as first parameter
  // which is returned by webpack(...) without callback.
  entry: "...",
  output: {
      path: "/"
      // no real path is required, just pass "/"
      // but it will work with other paths too.
  }
}), {
  // all options optional

  noInfo: false,
  // display no info to console (only warnings and errors)

  quiet: false,
  // display nothing to the console

  lazy: true,
  // switch into lazy mode
  // that means no watching, but recompilation on every request

  watchDelay: 300,
  // delay after change (only lazy: false)

  publicPath: "/assets/",
  // public path to bind the middleware to
  // use the same as in webpack

  headers: { "X-Custom-Header": "yes" },
  // custom headers

  stats: {
      colors: true
  }
  // options for formating the statistics
}));
    原文作者:奋进的小莫
    原文地址: https://segmentfault.com/a/1190000005742143
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞