平時本身用的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 }));