This file contains 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 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 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 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 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:pass@host.com: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 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 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 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 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); |
NewerOlder