Skip to content

Instantly share code, notes, and snippets.

@iksose
iksose / restAPI.markdown
Last active September 21, 2023 06:39
Creating a REST API using Node.js, Express, and MongoDB

###Creating a REST API using Node.js, Express, and MongoDB

####Installing Node.js

Go to http://nodejs.org, and click the Install button. Run the installer that you just downloaded. When the installer completes, a message indicates that Node was installed at /usr/local/bin/node and npm was installed at /usr/local/bin/npm. At this point node.js is ready to use. Let’s implement the webserver application from the nodejs.org home page. We will use it as a starting point for our project: a RESTful API to access data (retrieve, create, update, delete) in a wine cellar database.

Create a folder named nodecellar anywhere on your file system. In the wincellar folder, create a file named server.js.

Generators vs Fibers

Both ES6 generators and node-fibers can be used to block a coroutine while waiting on some I/O without blocking the entire process. Both can do this for arbitrarily deep call stacks. The main difference between the capabilities of the two is how explicit the syntax is.

Generators - Safe, but Explicit

In code that uses ES6 generators:

var run = require('gen-run'); // https://github.com/creationix/gen-run
global
daemon
defaults
mode http
timeout connect 86400000
timeout server 86400000
timeout client 86400000
timeout check 5s
javascript:( function() {
console.group( 'Performance Information for all entries of ' + window.location.href );
console.log( '\n-> Duration is displayed in ms\n ' )
var entries = window.performance.getEntries();
entries = entries.sort( function( a, b ) {
return b.duration - a.duration;
} );
function inNetwork(){
var thingie = 0
var next = function(){
console.log("herp", thingie)
thingie++
if(thingie < 10)
//registers next
setTimeout(next, 0);
}
//registers first
@iksose
iksose / gist:feeb9693d5d45a234095
Last active August 29, 2015 14:02
atom angular.js atom syntax
{
"match": "(?<!\\.)\\b(.forEach|.then)(?!\\s*:)\\b",
"name": "entity.name.function.js"
},
// matches .then & .forEach
{
"match": "(?<=\\).)(t(he(n)))\\b",
"name": "entity.name.function.js"
var OAuth2 = require('OAuth').OAuth2;
var https = require('https');
var oauth2 = new OAuth2(KEY, SECRET, 'https://api.twitter.com/', null, 'oauth2/token', null);
oauth2.getOAuthAccessToken('', {
'grant_type': 'client_credentials'
}, function (e, access_token) {
console.log(access_token); //string that we can use to authenticate request
var options = {
<ul>
<li ng-repeat="item in items | orderObjectBy:'color':true">{{ item.color }}</li>
</ul>
@iksose
iksose / uri.js
Created August 24, 2014 15:39 — forked from jlong/uri.js
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
@iksose
iksose / gist:4ca3772785f54005123a
Last active August 29, 2015 14:12
es6 symbols
const privateFoo = Symbol("foo");
function Public() {
this[privateFoo] = "bar";
}
Public.prototype.method = function () {
// Do stuff with `this[privateFoo]`...
};