├── README.md ├── async ├── 1.txt ├── 2.txt ├── 3.txt └── demo1.js ├── dynamic_module ├── app.js ├── bin │ └── www ├── controllers │ ├── a.js │ └── b.js ├── load_dir.js ├── package.json ├── public │ └── stylesheets │ │ └── style.css ├── routes │ ├── index.js │ └── users.js └── views │ ├── error.ejs │ └── index.ejs └── yield ├── demo1.js ├── demo2.js ├── demo3.js └── demo4.js /README.md: -------------------------------------------------------------------------------- 1 | # nodejs-cookbook 2 | nodejs-cookbook demo 3 | -------------------------------------------------------------------------------- /async/1.txt: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /async/2.txt: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /async/3.txt: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /async/demo1.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | 3 | async = { 4 | map: function (arrs, iter, callback) { 5 | var length = arrs.length; 6 | var count = 0; 7 | var result = []; 8 | 9 | var next = function (err, data) { 10 | if (err) { 11 | return callback(err); 12 | } 13 | 14 | result.push(data); 15 | count += 1; 16 | 17 | if (count === length) { 18 | return callback(err, result); 19 | } 20 | }; 21 | 22 | arrs.forEach(function (arr) { 23 | iter.call(null, arr, next); 24 | }) 25 | } 26 | 27 | }; 28 | 29 | async.map(['1.txt', '2.txt', '3.txt'], function (filename, next) { 30 | fs.readFile(filename, 'utf-8', next); 31 | }, function (err, result) { 32 | console.log(result); 33 | console.log('fin'); 34 | }); 35 | 36 | -------------------------------------------------------------------------------- /dynamic_module/app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var path = require('path'); 3 | var favicon = require('static-favicon'); 4 | var logger = require('morgan'); 5 | var cookieParser = require('cookie-parser'); 6 | var bodyParser = require('body-parser'); 7 | 8 | var routes = require('./routes/index'); 9 | var users = require('./routes/users'); 10 | 11 | var app = express(); 12 | 13 | // view engine setup 14 | app.set('views', path.join(__dirname, 'views')); 15 | app.set('view engine', 'ejs'); 16 | 17 | app.use(favicon()); 18 | app.use(logger('dev')); 19 | app.use(bodyParser.json()); 20 | app.use(bodyParser.urlencoded()); 21 | app.use(cookieParser()); 22 | app.use(express.static(path.join(__dirname, 'public'))); 23 | 24 | app.use('/', routes); 25 | app.use('/users', users); 26 | 27 | /// catch 404 and forwarding to error handler 28 | app.use(function(req, res, next) { 29 | var err = new Error('Not Found'); 30 | err.status = 404; 31 | next(err); 32 | }); 33 | 34 | /// error handlers 35 | 36 | // development error handler 37 | // will print stacktrace 38 | if (app.get('env') === 'development') { 39 | app.use(function(err, req, res, next) { 40 | res.status(err.status || 500); 41 | res.render('error', { 42 | message: err.message, 43 | error: err 44 | }); 45 | }); 46 | } 47 | 48 | // production error handler 49 | // no stacktraces leaked to user 50 | app.use(function(err, req, res, next) { 51 | res.status(err.status || 500); 52 | res.render('error', { 53 | message: err.message, 54 | error: {} 55 | }); 56 | }); 57 | 58 | 59 | module.exports = app; 60 | -------------------------------------------------------------------------------- /dynamic_module/bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var debug = require('debug')('my-application'); 3 | var app = require('../app'); 4 | 5 | app.set('port', process.env.PORT || 3000); 6 | 7 | var server = app.listen(app.get('port'), function() { 8 | debug('Express server listening on port ' + server.address().port); 9 | }); 10 | -------------------------------------------------------------------------------- /dynamic_module/controllers/a.js: -------------------------------------------------------------------------------- 1 | 2 | exports.index = function (req, res) { 3 | return res.send('a'); 4 | } 5 | -------------------------------------------------------------------------------- /dynamic_module/controllers/b.js: -------------------------------------------------------------------------------- 1 | 2 | exports.index = function (req, res) { 3 | return res.send('a'); 4 | } 5 | -------------------------------------------------------------------------------- /dynamic_module/load_dir.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var path = require('path'); 3 | 4 | var load = function(path, name) { 5 | if (name) { 6 | return require(path + name); 7 | } 8 | return require(path) 9 | }; 10 | 11 | module.exports = function (dir) { 12 | patcher = {} 13 | 14 | fs.readdirSync(__dirname + '/' + dir).forEach(function (filename) { 15 | if (!/\.js$/.test(filename)) { 16 | return; 17 | } 18 | var name = path.basename(filename, '.js'); 19 | var _load = load.bind(null, './' + dir + '/', name); 20 | 21 | patcher.__defineGetter__(name, _load); 22 | }); 23 | 24 | return patcher; 25 | } 26 | -------------------------------------------------------------------------------- /dynamic_module/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "application-name", 3 | "version": "0.0.1", 4 | "private": true, 5 | "scripts": { 6 | "start": "node ./bin/www" 7 | }, 8 | "dependencies": { 9 | "express": "~4.0.0", 10 | "static-favicon": "~1.0.0", 11 | "morgan": "~1.0.0", 12 | "cookie-parser": "~1.0.1", 13 | "body-parser": "~1.0.0", 14 | "debug": "~0.7.4", 15 | "ejs": "~0.8.5" 16 | } 17 | } -------------------------------------------------------------------------------- /dynamic_module/public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 50px; 3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; 4 | } 5 | 6 | a { 7 | color: #00B7FF; 8 | } -------------------------------------------------------------------------------- /dynamic_module/routes/index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | var loadDir = require('../load_dir'); 5 | 6 | var Controller = loadDir('controllers'); 7 | 8 | /* GET home page. */ 9 | router.get('/', function(req, res) { 10 | res.render('index', { title: 'Express' }); 11 | }); 12 | 13 | router.get('/a', Controller.a.index); 14 | router.get('/b', Controller.b.index); 15 | 16 | module.exports = router; 17 | -------------------------------------------------------------------------------- /dynamic_module/routes/users.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET users listing. */ 5 | router.get('/', function(req, res) { 6 | res.send('respond with a resource'); 7 | }); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /dynamic_module/views/error.ejs: -------------------------------------------------------------------------------- 1 |
<%= error.stack %>4 | -------------------------------------------------------------------------------- /dynamic_module/views/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
Welcome to <%= title %>
10 | 11 | 12 | -------------------------------------------------------------------------------- /yield/demo1.js: -------------------------------------------------------------------------------- 1 | function* Hello () { 2 | yield 1; 3 | yield 2; 4 | } 5 | 6 | var hello = Hello(); 7 | 8 | var a = hello.next(); 9 | var b = hello.next(); 10 | var c = hello.next(); 11 | 12 | console.log(a, b, c); 13 | -------------------------------------------------------------------------------- /yield/demo2.js: -------------------------------------------------------------------------------- 1 | function co(GenFunc) { 2 | return function (cb) { 3 | var gen = GenFunc(); 4 | next(); 5 | 6 | function next() { 7 | if (gen.next) { 8 | var ret = gen.next(); 9 | 10 | if (ret.done) { 11 | cb && cb(); 12 | } else { 13 | ret.value(next); 14 | } 15 | } 16 | } 17 | } 18 | } 19 | 20 | function delay(time) { 21 | return function (cb) { 22 | setTimeout(function () { 23 | cb(); 24 | }, time) 25 | } 26 | } 27 | 28 | console.time('1'); 29 | co(function* () { 30 | yield delay(200); 31 | yield delay(1000); 32 | yield delay(200); 33 | 34 | })(function () { 35 | console.timeEnd('1'); 36 | }); 37 | -------------------------------------------------------------------------------- /yield/demo3.js: -------------------------------------------------------------------------------- 1 | function co(GenFunc) { 2 | return function (cb) { 3 | var gen = GenFunc(); 4 | next(); 5 | 6 | function next(args) { 7 | if (gen.next) { 8 | var ret = gen.next(args); 9 | 10 | if (ret.done) { 11 | cb && cb(args); 12 | } else { 13 | ret.value(next); 14 | } 15 | } 16 | } 17 | } 18 | } 19 | 20 | function delay(time) { 21 | return function (fn) { 22 | setTimeout(function () { 23 | fn(time); 24 | }, time) 25 | } 26 | } 27 | 28 | co(function* () { 29 | var a; 30 | a = yield delay(200); 31 | a = yield delay(a + 100); 32 | a = yield delay(a + 100); 33 | })(function (data) { 34 | console.log(data); 35 | }); 36 | -------------------------------------------------------------------------------- /yield/demo4.js: -------------------------------------------------------------------------------- 1 | var Iterator = function (data) { 2 | this.curr = 0; 3 | this.data = data; 4 | } 5 | 6 | Iterator.prototype.hasNext = function () { 7 | return (this.data.length - 1) > curr; 8 | } 9 | 10 | Iterator.prototype.next = function () { 11 | var ret; 12 | if (!this.hasNext) { 13 | return; 14 | } 15 | 16 | ret = this.data[this.curr]; 17 | this.curr += 1; 18 | return ret; 19 | } 20 | 21 | 22 | var arrs = [1,2,3,4,5]; 23 | var iterator = new Iterator(arrs); 24 | 25 | for (var i = 0; i < arrs.length + 1; i++) { 26 | //1 27 | //2 28 | //3 29 | //4 30 | //5 31 | //undefined 32 | console.log(iterator.next()); 33 | } 34 | --------------------------------------------------------------------------------