-
-
Save pierrybos/6315182 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function soma (a, b) { | |
return a + b | |
} | |
module.exports = soma |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventEmitter = require('events').EventEmitter | |
var dog = new EventEmitter() | |
dog.on('bark', function () { | |
console.log('WOOF') | |
}) | |
setInterval(function(){ | |
dog.emit('bark') | |
}, 1000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. stdin | |
process.stdin.on('data', function (data) { | |
console.log(data.toString()) | |
}) | |
// 2. fs | |
var fs = require('fs') | |
, stream = fs.createReadStream('cats.txt') | |
stream.on('data', function (data) { | |
console.log(data.toString().toUpperCase()) | |
}) | |
// 3. pipe | |
stream.pipe(process.stdout) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http') | |
// Simple write() to stream | |
var server = http.createServer() | |
server.on('request', function (req, res) { | |
res.write('hello') | |
res.end() | |
}) | |
server.listen(8881) | |
console.log('listening on port 8881') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http') | |
// inspecting request properties | |
var server = http.createServer() | |
server.on('request', function (req, res) { | |
console.log(req.headers) | |
res.write(req.url) | |
res.end() | |
}) | |
server.listen(8882) | |
console.log('listening on port 8882') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http') | |
, fs = require('fs') | |
// file streaming | |
var server = http.createServer() | |
server.on('request', function (req, res) { | |
fs.createReadStream('../cats.txt').pipe(res) | |
}) | |
server.listen(8883) | |
console.log('listening on port 8883') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
// Express com logger, router e static | |
var app = express() | |
app.configure(function (){ | |
app.use(express.logger('dev')) | |
app.use(app.router) | |
app.use(express.static(__dirname + "/public")) | |
}) | |
app.get('/', function (req, res) { | |
res.end('index') | |
}) | |
app.listen('8888') | |
console.log('listening on port 8888') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
, hbs = require('express3-handlebars') | |
// Express with handlebars templates | |
var app = express() | |
app.configure(function (){ | |
app.engine('hbs', hbs()) | |
app.set('view engine', 'hbs') | |
app.use(express.logger('dev')) | |
app.use(app.router) | |
app.use(express.static(__dirname + "/public")) | |
if (app.get('env') === 'development') { | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
} | |
}) | |
app.get('/', function (req, res) { | |
// res.end('index') | |
res.render('index') | |
}) | |
app.get('/hello/:name', function (req, res) { | |
res.render('hello', { name: req.params.name }) | |
}) | |
app.listen('8888') | |
console.log('listening on port 8888') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http') | |
, fs = require('fs') | |
, express = require('express') | |
, hbs = require('express3-handlebars') | |
, sio = require('socket.io') | |
// socket.io + client-side events + pause stream | |
var app = express() | |
, server = http.createServer(app) | |
, io = sio.listen(server) | |
app.configure(function (){ | |
app.engine('hbs', hbs()) | |
app.set('view engine', 'hbs') | |
app.use(express.logger('dev')) | |
app.use(app.router) | |
app.use(express.static(__dirname + "/public")) | |
if (app.get('env') === 'development') { | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
} | |
}) | |
app.get('/', function (req, res) { | |
res.render('typewriter') | |
}) | |
io.sockets.on('connection', function (socket) { | |
var stream = fs.createReadStream('../cats.txt') | |
, chunk | |
, reading = true | |
stream.setEncoding('utf8') | |
function read () { | |
chunk = stream.read(1) | |
if (chunk) { | |
socket.emit('char', chunk) | |
if (reading) { | |
setTimeout(read, 100) | |
} | |
} | |
} | |
stream.pause() | |
stream.on('readable', read) | |
socket.on('pause', function () { | |
reading = false | |
}) | |
socket.on('continue', function () { | |
reading = true | |
read() | |
}) | |
}) | |
server.listen('8888') | |
console.log('listening on port 8888') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http') | |
, fs = require('fs') | |
, express = require('express') | |
, hbs = require('express3-handlebars') | |
, sio = require('socket.io') | |
// socket.io + file reading | |
var app = express() | |
, server = http.createServer(app) | |
, io = sio.listen(server) | |
app.configure(function (){ | |
app.engine('hbs', hbs()) | |
app.set('view engine', 'hbs') | |
app.use(express.logger('dev')) | |
app.use(app.router) | |
app.use(express.static(__dirname + "/public")) | |
if (app.get('env') === 'development') { | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
} | |
}) | |
app.get('/', function (req, res) { | |
res.render('typewriter') | |
}) | |
io.sockets.on('connection', function (socket) { | |
var stream = fs.createReadStream('../cats.txt') | |
, chunk | |
stream.setEncoding('utf8') | |
function read () { | |
chunk = stream.read(1) | |
if (chunk) { | |
socket.emit('char', chunk) | |
setTimeout(read, 200) | |
} | |
} | |
stream.pause() | |
stream.on('readable', read) | |
}) | |
server.listen('8888') | |
console.log('listening on port 8888') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http') | |
, express = require('express') | |
, hbs = require('express3-handlebars') | |
, sio = require('socket.io') | |
// simple socket.io demo | |
var app = express() | |
, server = http.createServer(app) | |
, io = sio.listen(server) | |
app.configure(function (){ | |
app.engine('hbs', hbs()) | |
app.set('view engine', 'hbs') | |
app.use(express.logger('dev')) | |
app.use(app.router) | |
app.use(express.static(__dirname + "/public")) | |
if (app.get('env') === 'development') { | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
} | |
}) | |
app.get('/', function (req, res) { | |
res.render('socketio') | |
}) | |
io.sockets.on('connection', function (socket) { | |
var counter = 0 | |
var interval = setInterval(function(){ | |
socket.emit('tick', counter) | |
counter++ | |
}, 500) | |
socket.on('disconnect', function () { | |
clearInterval(interval) | |
}) | |
}) | |
server.listen('8888') | |
console.log('listening on port 8888') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment