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 / serverApp
Created April 25, 2015 09:46
good implementation of server in nodejs
"use strict";
var http = require("http");
var https = require("https");
/**
* The default port that node servers bind to.
*/
var DEFAULT_PORT = 5000;
@krazylearner
krazylearner / Reading_writing_text_files
Created April 14, 2015 12:35
Reading and writing text files
In JDK 7, the most important classes for text files are:
Paths and Path - file locations/names, but not their content.
Files - operations on file content.
StandardCharsets and Charset (an older class), for encodings of text files.
the File.toPath method, which lets older code interact nicely with the newer java.nio API.
In addition, the following classes are also commonly used with text files, for both JDK 7 and earlier versions:
Scanner - allows reading files in a compact way
BufferedReader - readLine
BufferedWriter - write + newLine
When reading and writing text files:
@krazylearner
krazylearner / how-to-module
Last active August 29, 2015 14:19
These are some basic steps for writing a NodeJS module. Most of the suggestions in this document are optional. . This is a set of patterns that noders have found to work for them and their projects.
##Use Git
Most people in the node community use git for all their version control needs. It is an extremely powerful and robust tool. If you don't already use it, you should.
Don't wait until your program is "ready" before using git on it! Run git init in the root of your project folder right away, and commit your changes as you make them. It is a good habit, and can help avoid a lot of painful mishaps.
If you wish to share your program with others, then github is also a tremendously useful resource that most nodejs developers use.
A package.json File
=========================
WebSockets vs. Server-Sent events/EventSource
---------------------------------------------
Both WebSockets and Server-Sent Events are capable of pushing data to browsers. To me they seem to be competing technologies.
What is the difference between them? When would you choose one over the other?
Websockets and SSE (Server Sent Events) are both capable of pushing data to browsers, however they are not competing technologies.
Websockets connections can both send data to the browser and receive data from the browser.
A good example of an application that could use websockets is a chat application.
/*
What is "this"?
In all programming languages, there is this idea of current scope and current context. In JavaScript we have a lexical scope and a current "this" context.
In JavaScript all new scopes are created through "function" definitions. But contrary to other c-like languages, this is the only way to make a new scope. For loops don't do it, if blocks don't do it, plain curly braces assuredly don't do it. This simplicity is both a blessing and a curse. First let's have a couple of examples to explain creating scopes.
This is an example of global scope:
*/
// Pattern
//Usually, you call a callback as the last thing you do inside a function.
//You might consider it synonymous with return, only JavaScript does
//not halt function execution when it hits it. It can be easy to accidentally
//let execution continue after calling a callback, when you really expected it to end.
//In order to make this easy to spot, and make sure execution stops in the way you expect,
//I recommend returning the callback function call.
// wrong!