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 / 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 / .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 / 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 / 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 / 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 / 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 / frontendDevlopmentBookmarks.md
Created May 28, 2016 12:31 — forked from dypsilon/frontendDevlopmentBookmarks.md
A badass list of frontend development resources I collected over time.
@krazylearner
krazylearner / random.js
Created June 27, 2016 11:40 — forked from kerimdzhanov/random.js
Javascript get random number in a specific range
/**
* Get a random floating point number between `min` and `max`.
*
* @param {number} min - min number
* @param {number} max - max number
* @return {float} a random floating point number
*/
function getRandom(min, max) {
return Math.random() * (max - min) + min;
}
@krazylearner
krazylearner / documentsize.js
Created September 15, 2016 14:23
Testing the codument size with embedded array of 30000 Object ids
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/my_databasetest');
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var UserSchema = new Schema({
name: String,
friends: [{type: ObjectId}]

Show User

Returns json data about a single user.

  • URL

    /users/:id

  • Method: