var path = require('path');
var fs = require('fs');
var async = require('async');
function getFiles (dirPath, callback) {
fs.readdir(dirPath, function (err, files) {
if (err) return callback(err);
var filePaths = [];
async.eachSeries(files, function (fileName, eachCallback) {
var filePath = path.join(dirPath, fileName);
fs.stat(filePath, function (err, stat) {
if (err) return eachCallback(err);
if (stat.isDirectory()) {
getFiles(filePath, function (err, subDirFiles) {
if (err) return eachCallback(err);
filePaths = filePaths.concat(subDirFiles);
eachCallback(null);
});
} else {
if (stat.isFile() && /\.js$/.test(filePath)) {
filePaths.push(filePath);
}
eachCallback(null);
}
});
}, function (err) {
callback(err, filePaths);
});
});
}
getFiles('./', function (err, files) {
console.log(err || files);
});
var async = require('async');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
async.auto({
users: function (cb) {
connection.query("SELECT `id`, `name`, `isDude` FROM `users`", function(err, rows) {
cb(err, rows);
});
},
handle: ['users', function (cb, results) {
async.forEachOfSeries(results.users, function (user, key, mapCb) {
if (user.isDude) return mapCb();
connection.query("UPDATE `users` SET `isDude`=1 WHERE `id`=?", [user.id], mapCb);
}, cb);
}]
}, function (err) {
if (err) {
console.error(err);
} else {
console.log('All users is dudes, bro!');
}
connection.end();
});
var players = {
uryBa10dquiRVP77AAAD: { id: 'uryBa10dquiRVP77AAAD', x: 200, y: 200 },
PPF6HbYRf3dm30w1AAAE: { id: 'PPF6HbYRf3dm30w1AAAE', x: 200, y: 200 }
};
var players = [
{ id: 'uryBa10dquiRVP77AAAD', x: 200, y: 200 },
{ id: 'PPF6HbYRf3dm30w1AAAE', x: 200, y: 200 }
];
var async = require('async');
var mysqlConnection = ...;
function fetch (param, fetchCallback) { // fetchCallback (err, fetchedData)
async.auto({
queryOne: function (cb) {
// если нет параметра для первого запроса идём дальше
if (!param) return cb(null, null);
// сформировать_sql_запрос
var queryStr = "QUERY TYPE ONE " + mysqlConnection.escape(param);
cb(null, queryStr);
},
queryTwo: ['queryOne', function (cb, results) {
// если первый запрос сформирован сразу идём дальше
if (results.queryOne) return cb(null, null);
mysqlConnection.query("QUERY TO GET ADDITIONAL DATA", [...], function (err, data) {
if (err) return cb(err);
// сформировать_sql_запрос используя дополнительные данные из data
var additionalParam = data[0]&& data[0].field || 'An default';
var queryStr = "QUERY TYPE TWO BY ADDITIONAL DATA " + mysqlConnection.escape(additionalParam);
cb(null, queryStr);
});
}]
}, function (err, results) {
if (err) return fetchCallback(err);
mysqlConnection.query(results.queryOne || results.queryTwo, [...], fetchCallback);
});
}
// Экспрессовский app.get() приведён для демонстрации контекста потока
app.get('/users/:id', function (req, res, next) {
var id = req.params.id|0; // /users/1010 -> id = 1010;
mysql_connection.query("SELECT * FROM `Method_Arguments` WHERE `id`=?", [id],
function (err, result) {
if (err) return next(err);
if (result.length > 0) {
console.log(result);
} else {
console.log(id); // тут выводим id из WHERE
}
});
});
var T = [1, 2, '3', 4, 5];
connection.query("INSERT INTO `tasks` SET ?", {
login: req.body.login,
description: JSON.stringify(T),
type: 3,
time: new Date()
}, function(err) {
if (err) throw err;
});
var T = [1, 2, '3', 4, 5];
connection.query("INSERT INTO ?? SET ?", [
'tasks',
{
login: req.body.login,
description: JSON.stringify(T),
type: 3,
time: new Date()
}
], function(err) {
if (err) throw err;
});
--outDir - Redirect output structure to the directory.
$ export NODE_ENV=production
'use strict';
var node;
var gulp = require('gulp');
var spawn = require('child_process').spawn;
var JS_SERVER_BIN_FILE = './bin/server.js';
var JS_SERVER_SOURCE = './js/server/**/*.js';
gulp.task('supervise', function() {
gulp.watch(JS_SERVER_SOURCE, function() {
if (node) node.kill();
node = spawn('node', [JS_SERVER_BIN_FILE], {stdio: 'inherit'});
node.on('close', function (code) {
if (code === 8) {
console.log('Error detected, waiting for changes...');
}
});
});
});
process.on('exit', function() {
if (node) node.kill()
});