平时本身用的npm模块也不算少了,其实网上有不少牛人开发的npm模块都很好,但愿不要被埋没了。node
做用:获取最新可用的迅雷 vip 帐号。
解决什么:不用每次打开网站去找号。。
用法: $ thunder
orwebpack
var thunderVip = require('thunder-vip'); thunderVip(function (err, accounts) { console.log(accounts); });
截图 git
npm-user-downloads github
查看 npm 用户某个时间段内全部模块的下载量,按从高到低排名。web
解决什么:起初是想看本身的那些模块下载量多,好重点维护。。npm
用法: $ nud hupengbest last-month --limit=20
json
截图:windows
做用:异步控制 api
1 栗子promise
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) { });
npm install debug
使用方法
//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);
效果图
在windows环境下须要设置环境变量set DEBUG=*,-not_this
我这里使用的是idea的debug调试
windows 下启动方式
将debug日志转存到文件中 DEBUG_FD=3 node your-app.js 3> whatever.log
使用方法
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
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' }; });
使用方法
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 }));