Skip to content

Instantly share code, notes, and snippets.

@makefunstuff
Forked from kristopherjohnson/nodejs_snippets.js
Created November 8, 2013 10:46
Show Gist options
  • Save makefunstuff/7369349 to your computer and use it in GitHub Desktop.
Save makefunstuff/7369349 to your computer and use it in GitHub Desktop.
// Common modules
var util = require('util');
var events = require('events');
var http = require('http');
var url = require('url');
var fs = require('fs');
var zlib = require('zlib');
// Simple HTTP server
var s = http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
});
s.listen(8124);
// Simple Express server
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
// socket.io server
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
// socket.io server with Express
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(80);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
// socket.io client
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
// Simple HTTP request
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // Print the google web page.
}
});
// Parse a URL
var p = url.parse('http://undefinedvalue.com/foo/bar?baz=1');
console.log('Query: ' + p.query);
// Parse XML and display as pretty-printed JSON
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
parser.parseString(myXmlData, function (error, result) {
if (error) {
// ...
}
else {
console.log(JSON.stringify(result, null, " ");
}
});
// Format a string
str = util.format('Hello, %s! It is %d', 'world', 2012);
// Handle exception that bubbles up to event loop
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
// Schedule execution in the future
var t = setTimeout(function () {
console.log('It is five seconds later');
}, 5000);
// Cancel a setTimeout() callback
clearTimeout(t);
// Schedule/cancel repeating timer
var interval = setInterval(function() {
console.log("Once-per-second timer");
}, 1000);
clearInterval(interval);
// Run on next execution of event loop
process.nextTick(function () {
console.log('nextTick callback');
});
// Create a "subclass" of EventEmitter
function MyStream() {
events.EventEmitter.call(this);
}
util.inherits(MyStream, events.EventEmitter);
MyStream.prototype.write = function(data) {
this.emit("data", data);
}
// Handle events on a readable stream
s.on('data', function (data) { }); // data is Buffer or string
s.on('end', function () { });
s.on('error', function (exception) { });
s.on('close', function () { });
// Handle events on a writable stream
s.on('drain', function () { });
s.on('error', function (exception) { });
s.on('close', function () { });
s.on('pipe', function (src) { });
// Compress a file
var gzip = zlib.createGzip();
var inp = fs.createReadStream('input.txt');
var out = fs.createWriteStream('input.txt.gz');
inp.pipe(gzip).pipe(out);
// Mocha tests (BDD-style)
var assert = require('assert');
describe('Test', function () {
it('should pass', function () {
assert.equal(1, 1);
});
it('should fail', function () {
assert.equal(1, 0);
});
});
// Mocha tests (TDD-style)
suite('Test', function () {
test('should pass', function () {
assert.equal(1, 1);
});
test('should fail', function () {
assert.equal(1, 0);
});
});
// should.js
var should = require('should');
should.exist({})
should.exist([])
should.exist('')
should.exist(0)
should.exist(null) // will throw
should.exist(undefined) // will throw
should.not.exist(undefined)
should.not.exist(null)
should.not.exist('') // will throw
should.not.exist({}) // will throw
true.should.be.ok
'yay'.should.be.ok
(1).should.be.ok
false.should.not.be.ok
''.should.not.be.ok
(0).should.not.be.ok
true.should.be.true
'1'.should.not.be.true
false.should.be.false
(0).should.not.be.false
var args = (function(){ return arguments; })(1,2,3);
args.should.be.arguments;
[].should.not.be.arguments;
[].should.be.empty
''.should.be.empty
({ length: 0 }).should.be.empty
({ foo: 'bar' }).should.eql({ foo: 'bar' })
[1,2,3].should.eql([1,2,3])
should.strictEqual(undefined, value)
should.strictEqual(false, value)
(4).should.equal(4)
'test'.should.equal('test')
[1,2,3].should.not.equal([1,2,3])
user.age.should.be.within(5, 50)
user.should.be.a('object')
'test'.should.be.a('string')
user.should.be.an.instanceof(User)
[].should.be.an.instanceOf(Array)
user.age.should.be.above(5)
user.age.should.not.be.above(100)
user.age.should.be.below(100)
user.age.should.not.be.below(5)
username.should.match(/^\w+$/)
user.pets.should.have.length(5)
user.pets.should.have.a.lengthOf(5)
user.should.have.property('name')
user.should.have.property('age', 15)
user.should.not.have.property('rawr')
user.should.not.have.property('age', 0)
({ foo: 'bar' }).should.have.ownProperty('foo')
res.should.have.header('content-length');
res.should.have.header('content-length', '123');
res.should.be.json
res.should.be.html
[1,2,3].should.include(3)
[1,2,3].should.not.include(4)
'foo bar baz'.should.include('baz')
'foo bar baz'.should.not.include('FOO')
[[1],[2],[3]].should.includeEql([2])
[[1],[2],[3]].should.not.includeEql([4])
(function(){
throw new Error('fail');
}).should.throw();
(function(){
throw new Error('fail');
}).should.throw('fail');
(function(){
throw new Error('failed to foo');
}).should.throw(/^fail/);
var obj = { foo: 'bar', baz: 'raz' };
obj.should.have.keys('foo', 'bar');
obj.should.have.keys(['foo', 'bar']);
(1).should.eql(0, 'some useful description')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment