Skip to content

Instantly share code, notes, and snippets.

Whether you use 2 spaces or 4 spaces, there are a few simple things that can make your node.js code easier to read. We've been using them in all the hapi modules for over 4 years now to great results. This list is by no means complete but it highlights the most useful elements that will give you immediate value in reducing bugs.

Required modules

JavaScript makes it harder than most languages to know where variables are coming from. Variables assigned required modules are particularly important because they represent a singleton object shared with the entire application. There are also globals and module globals, along with function variables and arguments.

Traditionally, variables starting with an uppercase letter represent a class that must be instantiated using new. This was an important semantic in the early days of JavaScript but at this point, if you don't know Date requires new Date() you are probably very new. We have adopted Upper Camel Case variable names for all module global variables

@OzieWest
OzieWest / iocContainer.cs
Created March 6, 2015 08:58
Own IoC Container
public class MyContainer
{
private readonly Dictionary<Type, Type> _registrations;
public MyContainer()
{
_registrations = new Dictionary<Type, Type>();
}
public void Register<T, TS>()
@OzieWest
OzieWest / clone.js
Last active August 29, 2015 14:18
JavaScript clone function
function clone(obj) {
if(obj == null || typeof(obj) != 'object')
return obj;
var temp = obj.constructor();
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
temp[key] = clone(obj[key]);
}
@OzieWest
OzieWest / _vimrc
Last active February 19, 2016 08:51
VIM config
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
set nobackup
set nowritebackup
set number
set tabstop=4
set shiftwidth=4
#!/bin/bash
apt-get install -y curl unzip
mkdir -p /var/lib/consul
mkdir -p /usr/share/consul
mkdir -p /etc/consul/conf.d
curl -OL https://dl.bintray.com/mitchellh/consul/0.3.1_linux_amd64.zip
unzip 0.3.1_linux_amd64.zip
mv consul /usr/local/bin/consul
@OzieWest
OzieWest / .vimrc
Last active February 10, 2016 13:08
.vimrc
set tabstop=4
set shiftwidth=4
set smarttab
set et
set nowrap
set ai
set cin
set showmatch
set hlsearch
set incsearch
@OzieWest
OzieWest / 01_file.json
Created March 24, 2016 21:56
Dockerrun.aws.json
{
"AWSEBDockerrunVersion": "1",
"Authentication": {
"Bucket": "yourbucketname",
"Key": ".dockercfg.json"
},
"Image": {
"Name": "umitunal/spring-boot-docker",
"Update": "true"
},
@OzieWest
OzieWest / .travis.yml
Created May 5, 2016 21:55 — forked from BretFisher/.travis.yml
Travis-CI Docker Image Build and Push to AWS ECR
sudo: required #is required to use docker service in travis
language: php #can be any language, just php for example
services:
- docker # required, but travis uses older version of docker :(
install:
- echo "install nothing!" # put your normal pre-testing installs here
@OzieWest
OzieWest / config_manager.js
Created May 17, 2016 09:48
node conf example
module.exports.getConfig = (argv) => {
return {
value: process.env.VALUE || argv['value'] || 'default'
}
};
@OzieWest
OzieWest / mongodb_native_query.js
Last active July 20, 2016 08:05
mongodb_native_query.js
// Пример ниже верен только для нативного монго драйвера - https://mongodb.github.io/node-mongodb-native
let cityId = <CITY_ID_HERE>;
let query = [
{
// найти город такой то
$match: { id: cityId }
},
{