Skip to content

Instantly share code, notes, and snippets.

@baniol
baniol / node_cli.js
Created April 25, 2014 19:41
node cli example
var path = require('path'),
optimist = require('optimist'),
spawn = require('child_process').spawn,
fs = require('fs'),
Clip = require('../kindle-my-clippings'),
deploySettings = require('../deploy-settings'),
options = require('../options');
var clip = new Clip(options);
@baniol
baniol / append_file.js
Created May 8, 2014 06:39
nodejs, appending data to file, write file
var writeData = JSON.stringify(data) + '@@||@@';
fs.appendFile('amq.log', writeData, function (err) {
// console.log(err);
});
@baniol
baniol / cfg_log4js.json
Created May 9, 2014 13:33
download file and log request with log4js, nodejs, http
{
"appenders": [
{
"type": "file",
"filename": "serverLog.log" ,
"category": "simplelog",
"backups" : 2,
"maxLogSize": 20480,
"description": "Example log."
}]
@baniol
baniol / strip_whitespaces.js
Created May 14, 2014 22:08
strip whitespaces, javascript, string
// Remove leading and trailing whitespace
// Requires jQuery
var str = " a b c d e f g ";
var newStr = $.trim(str);
// "a b c d e f g"
// Remove leading and trailing whitespace
// JavaScript RegEx
var str = " a b c d e f g ";
var newStr = str.replace(/(^\s+|\s+$)/g,'');
@baniol
baniol / center_box.js
Created May 17, 2014 06:41
center box, jquery plugin
/**
* jQuery utility for centering a div on the screen
*
* @static
* @method $.centerBox
*/
$.fn.centerBox = function () {
var t = this;
t.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop()) + "px");
t.css("left", Math.max(0, (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft()) + "px");
@baniol
baniol / transform.js
Created May 18, 2014 17:14
nodejs stream transform, uppercase
var Transform = require('stream').Transform;
/// Server:
var server = require('net').createServer();
function onConnection(socket) {
/// Create the transform stream:
var uppercase = new Transform({
@baniol
baniol / stream2.js
Created May 18, 2014 18:49
nodejs streams
// From: http://codewinds.com/blog/2013-08-04-nodejs-readable-streams.html
//Simple example of reading a file and echoing it to stdout:
var fs = require('fs');
var readStream = fs.createReadStream('../sources/bible.txt');
readStream.pipe(process.stdout);
// ====================================================================================
'use strict';
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
options: {
jshintrc: '.jshintrc'
},
all: [
'Gruntfile.js',
@baniol
baniol / copy-ssh-id.sh
Created June 7, 2014 20:23
copy ssh public key to a remote server
cat ~/.ssh/id_rsa.pub | ssh usert@host "cat >> ~/.ssh/authorized_keys"
@baniol
baniol / mongo_dump.sh
Created June 10, 2014 18:21
mongodb dump & dropbox upload, backup
#!/usr/bin/env bash
#Get current date
NOW="$(date +'%d-%m-%Y_%H-%M')"
# Settings:
# Path to a temporary directory
DIR=/root/mongodb_dump/