Skip to content

Instantly share code, notes, and snippets.

View krazylearner's full-sized avatar
🏠
Working from home

Ankur Bansal krazylearner

🏠
Working from home
View GitHub Profile
@krazylearner
krazylearner / antipattern2.js
Created April 29, 2016 13:09
A drawback of 1.1. is that the method getInfo() is recreated every time you create a new object. Sometimes that may be what you want, but it's rare. A more inexpensive way is to add getInfo() to the prototype of the constructor function.
/////////////antipattern///////////
function Apple (type) {
this.type = type;
this.color = "red";
this.getInfo = function() {
return this.color + ' ' + this.type + ' apple';
};
}
///////////CORRECT///////////
@krazylearner
krazylearner / antipattern.js
Last active May 4, 2016 13:37
In the example above you see that the method getInfo() of the Apple "class" was defined in a separate function getAppleInfo(). While this works fine, it has one drawback – you may end up defining a lot of these functions and they are all in the "global namespece". This means you may have naming conflicts if you (or another library you are using)…
//1
///////////Antipattern///////////
function Apple (type) {
this.type = type;
this.color = "red";
// anti-pattern!
this.getInfo = getAppleInfo;
}
// anti-pattern!
@krazylearner
krazylearner / node-port-forwarding.txt
Created March 27, 2016 20:20
redirect port 80 to port 3000 or any other in nodejs environment on ubuntu
What I do on my cloud instances is I redirect port 80 to port 3000 with this command:
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
Then I launch my Node.js on port 3000. Requests to port 80 will get mapped to port 3000.
You should also edit your /etc/rc.local file and add that line minus the sudo. That will add the redirect when the machine boots up. You don't need sudo in /etc/rc.local because the commands there are run as root when the system boots.
Logs
Use the forever module to launch your Node.js with. It will make sure that it restarts if it ever crashes and it will redirect console logs to a file.
@krazylearner
krazylearner / random.js
Created March 25, 2016 21:18
random token generator in nodejs synchronously
// generates 4 bytes token
var outname = require('crypto').randomBytes(4).toString('hex');
@krazylearner
krazylearner / .htaccess
Created February 13, 2016 14:57
The requested resource / is no longer available on this server
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Redirect 410 /
</IfModule>
@krazylearner
krazylearner / constructor_design.md
Last active October 12, 2015 13:23
Is it bad practice to have a constructor function return a Promise?

Yes, it is a bad practise. A constructor should return an instance of its class, nothing else. It would mess up the new operator and inheritance otherwise.

Moreover, a constructor should only create and initialize a new instance. It should set up data structures and all instance-specific properties, but not execute any tasks. It should be a pure function without side effects if possible, with all the benefits that has.

What if I want to execute things from my constructor? That should go in a method of your class. You want to mutate global state? Then call that procedure explicitly, not as a side effect of generating an object. This call can go right after the instantiation:

var engine = new Engine()
engine.displayPosts();
@krazylearner
krazylearner / function_check.md
Created October 3, 2015 12:26
An example Consider a function that asynchronously connects to a TCP port at an IPv4 address. Here's an example of how we might document it:
/*
 * Make a TCP connection to the given IPv4 address.  Arguments:
 *
 *    ip4addr        a string representing a valid IPv4 address
 *
 *    tcpPort        a positive integer representing a valid TCP port
 *
 *    timeout        a positive integer denoting the number of milliseconds
@krazylearner
krazylearner / design_1.md
Last active October 3, 2015 11:49
nodejs design patterns applicable for other programming languages

##Patterns for writing functions ###how do you deliver errors to the code that called your function ?

''' Document what your function does, including what arguments it takes (including their types and any other constraints), what it returns, what errors can happen, and what those errors mean. So if you're writing a new function, you have to tell your callers what errors can happen and what they mean. '''

three basic patterns for a function to deliver errors.

Throw, Callback, or EventEmitter
@krazylearner
krazylearner / Semantic_Versioning.md
Last active August 29, 2015 14:24
Semantic Versioning 2.0.0

Summary

Given a version number MAJOR.MINOR.PATCH, increment the:

  1. MAJOR version when you make incompatible API changes,
  2. MINOR version when you add functionality in a backwards-compatible manner, and
  3. PATCH version when you make backwards-compatible bug fixes.

##Semantic Versioning Specification (SemVer)

@krazylearner
krazylearner / styles.md
Last active August 29, 2015 14:23
styles views layouts User interface PART 1

PART 1

  1. Styles and themes are time-saving ways to create a consistent look and feel across your Android application.
  2. Styles and themes are essentially the same thing: a collection of properties.
  3. These properties can be anything from button color to the "wrap content" attribute or the size of your text.
  4. The crucial difference is how they’re applied to your project:
  5. A style is applied to a View.
  6. A theme is applied to individual activities or an entire application.

Why Should I Use Themes and Styles?