Skip to content

Instantly share code, notes, and snippets.

View wwwy3y3's full-sized avatar
🎯
Focusing

william chang(張仲威) wwwy3y3

🎯
Focusing
View GitHub Profile
var mongoose = require('./db-connect'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId,
uuid = require('node-uuid'),
Validator = require('validator').Validator,
val = new Validator(),
bcrypt = require('bcrypt');
Validator.prototype.error = function(msg) { return false; };

Clustering: The basics

The trick? pass the file descriptor from a parent process and have the server.listen reuse that descriptor. So multiprocess in their own memory space (but with ENV shared usually)

It does not balance, it leaves it to the kernel.

In the last nodejs > 0.8 there is a cluster module (functional although marked experimental)

tl;dr

  1. Don't run as root.
  2. For sessions, set httpOnly (and secure to true if running over SSL) when setting cookies.
  3. Use the Helmet for secure headers: https://github.com/evilpacket/helmet
  4. Enable csrf for preventing Cross-Site Request Forgery: http://expressjs.com/api.html#csrf
  5. Don't use the deprecated bodyParser() and only use multipart explicitly. To avoid multiparts vulnerability to 'temp file' bloat, use the defer property and pipe() the multipart upload stream to the intended destination.
// population of all cities over 1 million people using the cities1000 dataset from http://geonames.org
var fs = require('fs');
var cities = fs.createReadStream(__dirname + '/cities1000.txt');
var split = require('split');
var through = require('through');
var fields = require('cities1000').fields;
var popIndex = fields.indexOf('population');
var sum = 0;
@wwwy3y3
wwwy3y3 / API.md
Created August 12, 2013 19:23 — forked from iros/API.md

Title

<Additional information about your API call. Try to use verbs that match both request type (fetching vs modifying) and plurality (one vs multiple).>

  • URL

    <The URL Structure (path only, no root url)>

  • Method:

var express = require('express');
var sys = require('util');
var oauth = require('oauth');
var app = express.createServer();
var _twitterConsumerKey = process.env['TWITTER_CONSUMER_KEY'];
var _twitterConsumerSecret = process.env['TWITTER_CONSUMER_SECRET'];
console.log("_twitterConsumerKey: %s and _twitterConsumerSecret %s", process.env['TWITTER_CONSUMER_KEY'], process.env['TWITTER_CONSUMER_SECRET']);
@wwwy3y3
wwwy3y3 / db_con.js
Last active December 16, 2015 09:28
async database connection manager, singleton pattern
var path= require('path');
//var cpath= path.resolve(__dirname, '..', '..', '..', 'node_modules', 'cassandra-client');
var Connection = require('cassandra-client').Connection;
var EventEmitter = require('events').EventEmitter;
/*
* four stages of connection
* 1. restart the server, connection is null
* 2. new Connection(options), but not yet connect, (client= null, doesn't have con, nothing in validators)

Twitter公式クライアントのコンシューマキー

Twitter for iPhone

Consumer key: IQKbtAYlXLripLGPWd0HUA
Consumer secret: GgDYlkSvaPxGxC4X8liwpUoqKwwr3lCADbz8A7ADU

Twitter for Android

Consumer key: 3nVuSoBZnx6U4vzUxf5w
Consumer secret: Bcs59EFbbsdF6Sl9Ng71smgStWEGwXXKSjYvPVt7qys

Twitter for iPad

Consumer key: CjulERsDeqhhjSme66ECg