npm模块分享

平时自己用的npm模块也不算少了,其实网上有很多牛人开发的npm模块都很好,希望不要被埋没了。

一、 实用的模块

1.thunder-vip

作用:获取最新可用的迅雷 vip 账号。
解决什么:不用每次打开网站去找号。。
用法: $ thunder or

1
2
3
4
5
  var thunderVip = require('thunder-vip');

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

截图

1

2. npm-user-downloads

npm-user-downloads

查看 npm 用户某个时间段内所有模块的下载量,按从高到低排名。

解决什么:起初是想看自己的那些模块下载量多,好重点维护。。

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

截图:

1

二、koa开发模块

1. co

作用:异步控制

1 栗子

1
2
3
4
5
6
7
8
	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*)

1
2
3
4
5
6
7
	var fn = co.wrap(function* (val) {
	  return yield Promise.resolve(val);
	});

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

	});

3 完整的example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
	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

1
2
3
4
5
6
7
	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

1
2
3
4
5
6
7
	var fn = co.wrap(function* (val) {
	  return yield Promise.resolve(val);
	});

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

	});

2、debug

npm install debug

  1. 使用方法
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
  //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');
1
2
3
4
5
6
 //Example worker.js:
	var debug = require('debug')('worker');

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

效果图

1

在windows环境下需要设置环境变量set DEBUG=*,-not_this 我这里使用的是idea的debug调试

1

windows 下启动方式

1

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

3、koa-bodyparser

使用方法

1
2
3
4
5
6
7
8
9
  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

使用方法

1
2
3
4
5
6
7
8
9
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
}));
署名 - 非商业性使用 - 禁止演绎 4.0