This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////Server//////////////////////////////////////// | |
var WebSocketServer = require("ws").Server; | |
var http = require("http"); | |
var express = require("express"); | |
var app = express(); | |
var port = process.env.PORT || 5000; | |
var server = http.createServer(app); | |
server.listen(port); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// immutable strings | |
var person = 'xyz'; | |
var anotherPerson = person; | |
person = 'abc'; | |
console.log(person); | |
console.log(anotherPerson); | |
// functions and other variables | |
var fun = function(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Careful with arrays | |
function Class(){ | |
this.array = []; | |
} | |
Class.prototype.anotherArray = []; | |
var obj = new Class(); | |
var anotherObj = new Class(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// all of exposed methods in os | |
// are basically informations about OS | |
var os = require('os'); | |
//console.log(os); | |
console.log(os.platform()); | |
console.log(os.arch()); | |
console.log(os.type()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = require('url'); | |
var str = 'http://user:[email protected]:8080/p/a/t/h?query=string#hash'; | |
// parse and format | |
var obj = url.parse(str,true); | |
console.log(url.format(obj)); | |
// resolve the browser will do | |
console.log(url.resolve('http://www.google.com/users','user')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// global object | |
console.log(process); | |
process.on('uncaughtException', function(err) { | |
console.log('Caught exception: ' + err); | |
}); | |
setTimeout(function() { | |
console.log('This will still run.'); | |
}, 500); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// writes to stdout, same as info | |
console.log('hello %d', 2015); | |
//writes to stderr, same as warn | |
console.error('hello %d', 2015); | |
console.dir({ | |
name: 'John doe', | |
address: { | |
city: 'NY', | |
state: 'DC' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
// list all status codes | |
http.STATUS_CODES | |
var server = http.createServer(function(req, res){ | |
// get header info | |
console.log(req.headers); | |
//get method |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventEmitter = require('events').EventEmitter | |
, util = require('util') | |
; | |
function Animal(){ | |
this.nose = 2; | |
this.ear = 4; | |
} | |
util.inherits(Animal, EventEmitter); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reference: http://www.html5rocks.com/en/tutorials/es6/promises/ | |
// `Promises` have been introduced natively as a part of ES6 now. In this blogpost hopefully I will | |
// try to explore all the aspects of `Promises`. | |
// So what is a Promise? | |
// `Promises` are merely a prettier way of writing code which makes an asynchronous code look like synchronous. | |
// Usual way of XHR | |
$.get("script.php", function(data) { | |
console.log('Your account has been created successfully!'); |
OlderNewer