base project
This commit is contained in:
26
node_modules/union/examples/after/index.js
generated
vendored
Normal file
26
node_modules/union/examples/after/index.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
union = require('../../lib');
|
||||
|
||||
var server = union.createServer({
|
||||
before: [ function (req,res) {
|
||||
if (req.url === "/foo") {
|
||||
res.text(201, "foo");
|
||||
}
|
||||
} ],
|
||||
after: [
|
||||
function LoggerStream() {
|
||||
var stream = new union.ResponseStream();
|
||||
|
||||
stream.once("pipe", function (req) {
|
||||
console.log({res: this.res.statusCode, method: this.req.method});
|
||||
});
|
||||
|
||||
return stream;
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
server.listen(9080);
|
||||
console.log('union running on 9080');
|
||||
|
BIN
node_modules/union/examples/simple/favicon.png
generated
vendored
Normal file
BIN
node_modules/union/examples/simple/favicon.png
generated
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 545 B |
96
node_modules/union/examples/simple/middleware/favicon.js
generated
vendored
Normal file
96
node_modules/union/examples/simple/middleware/favicon.js
generated
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
|
||||
/*!
|
||||
* Connect - favicon
|
||||
* Copyright(c) 2010 Sencha Inc.
|
||||
* Copyright(c) 2011 TJ Holowaychuk
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var crypto = require('crypto')
|
||||
, fs = require('fs');
|
||||
|
||||
/**
|
||||
* Favicon cache.
|
||||
*/
|
||||
|
||||
var icon;
|
||||
|
||||
/**
|
||||
* Return md5 hash of the given string and optional encoding,
|
||||
* defaulting to hex.
|
||||
*
|
||||
* utils.md5('wahoo');
|
||||
* // => "e493298061761236c96b02ea6aa8a2ad"
|
||||
*
|
||||
* @param {String} str
|
||||
* @param {String} encoding
|
||||
* @return {String}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.md5 = function (str, encoding) {
|
||||
return crypto
|
||||
.createHash('md5')
|
||||
.update(str)
|
||||
.digest(encoding || 'hex');
|
||||
};
|
||||
|
||||
/**
|
||||
* By default serves the connect favicon, or the favicon
|
||||
* located by the given `path`.
|
||||
*
|
||||
* Options:
|
||||
*
|
||||
* - `maxAge` cache-control max-age directive, defaulting to 1 day
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* connect.createServer(
|
||||
* connect.favicon()
|
||||
* );
|
||||
*
|
||||
* connect.createServer(
|
||||
* connect.favicon(__dirname + '/public/favicon.ico')
|
||||
* );
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {Object} options
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function favicon(path, options) {
|
||||
var options = options || {}
|
||||
, path = path || __dirname + '/../public/favicon.ico'
|
||||
, maxAge = options.maxAge || 86400000;
|
||||
|
||||
return function favicon(req, res, next) {
|
||||
if ('/favicon.ico' == req.url) {
|
||||
if (icon) {
|
||||
res.writeHead(200, icon.headers);
|
||||
res.end(icon.body);
|
||||
} else {
|
||||
fs.readFile(path, function (err, buf) {
|
||||
if (err) return next(err);
|
||||
icon = {
|
||||
headers: {
|
||||
'Content-Type': 'image/x-icon'
|
||||
, 'Content-Length': buf.length
|
||||
, 'ETag': '"' + exports.md5(buf) + '"'
|
||||
, 'Cache-Control': 'public, max-age=' + (maxAge / 1000)
|
||||
},
|
||||
body: buf
|
||||
};
|
||||
res.writeHead(200, icon.headers);
|
||||
res.end(icon.body);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
};
|
||||
};
|
26
node_modules/union/examples/simple/middleware/gzip-decode.js
generated
vendored
Normal file
26
node_modules/union/examples/simple/middleware/gzip-decode.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
var spawn = require('child_process').spawn,
|
||||
util = require('util'),
|
||||
RequestStream = require('../../lib').RequestStream;
|
||||
|
||||
var GzipDecode = module.exports = function GzipDecoder(options) {
|
||||
RequestStream.call(this, options);
|
||||
|
||||
this.on('pipe', this.decode);
|
||||
}
|
||||
|
||||
util.inherits(GzipDecode, RequestStream);
|
||||
|
||||
GzipDecode.prototype.decode = function (source) {
|
||||
this.decoder = spawn('gunzip');
|
||||
this.decoder.stdout.on('data', this._onGunzipData.bind(this));
|
||||
this.decoder.stdout.on('end', this._onGunzipEnd.bind(this));
|
||||
source.pipe(this.decoder);
|
||||
}
|
||||
|
||||
GzipDecoderStack.prototype._onGunzipData = function (chunk) {
|
||||
this.emit('data', chunk);
|
||||
}
|
||||
|
||||
GzipDecoderStack.prototype._onGunzipEnd = function () {
|
||||
this.emit('end');
|
||||
}
|
40
node_modules/union/examples/simple/middleware/gzip-encode.js
generated
vendored
Normal file
40
node_modules/union/examples/simple/middleware/gzip-encode.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
var spawn = require('child_process').spawn,
|
||||
util = require('util'),
|
||||
ResponseStream = require('../../lib').ResponseStream;
|
||||
|
||||
/**
|
||||
* Accepts a writable stream, i.e. fs.WriteStream, and returns a StreamStack
|
||||
* whose 'write()' calls are transparently sent to a 'gzip' process before
|
||||
* being written to the target stream.
|
||||
*/
|
||||
var GzipEncode = module.exports = function GzipEncode(options) {
|
||||
ResponseStream.call(this, options);
|
||||
|
||||
if (compression) {
|
||||
process.assert(compression >= 1 && compression <= 9);
|
||||
this.compression = compression;
|
||||
}
|
||||
|
||||
this.on('pipe', this.encode);
|
||||
}
|
||||
|
||||
util.inherits(GzipEncode, ResponseStream);
|
||||
|
||||
GzipEncode.prototype.encode = function (source) {
|
||||
this.source = source;
|
||||
};
|
||||
|
||||
GzipEncode.prototype.pipe = function (dest) {
|
||||
if (!this.source) {
|
||||
throw new Error('GzipEncode is only pipeable once it has been piped to');
|
||||
}
|
||||
|
||||
this.encoder = spawn('gzip', ['-'+this.compression]);
|
||||
this.encoder.stdout.pipe(dest);
|
||||
this.encoder.stdin.pipe(this.source);
|
||||
};
|
||||
|
||||
inherits(GzipEncoderStack, StreamStack);
|
||||
exports.GzipEncoderStack = GzipEncoderStack;
|
||||
|
||||
GzipEncoderStack.prototype.compression = 6;
|
60
node_modules/union/examples/simple/simple.js
generated
vendored
Normal file
60
node_modules/union/examples/simple/simple.js
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
union = require('../../lib'),
|
||||
director = require('director'),
|
||||
favicon = require('./middleware/favicon');
|
||||
|
||||
var router = new director.http.Router();
|
||||
|
||||
var server = union.createServer({
|
||||
before: [
|
||||
favicon(path.join(__dirname, 'favicon.png')),
|
||||
function (req, res) {
|
||||
var found = router.dispatch(req, res);
|
||||
if (!found) {
|
||||
res.emit('next');
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
router.get('/foo', function () {
|
||||
this.res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
this.res.end('hello world\n');
|
||||
});
|
||||
|
||||
router.post('/foo', { stream: true }, function () {
|
||||
var req = this.req,
|
||||
res = this.res,
|
||||
writeStream;
|
||||
|
||||
writeStream = fs.createWriteStream(__dirname + '/' + Date.now() + '-foo.txt');
|
||||
req.pipe(writeStream);
|
||||
|
||||
writeStream.on('close', function () {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('wrote to a stream!');
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/redirect', function () {
|
||||
this.res.redirect('http://www.google.com');
|
||||
});
|
||||
|
||||
router.get('/custom_redirect', function () {
|
||||
this.res.redirect('/foo', 301);
|
||||
});
|
||||
|
||||
router.get('/async', function () {
|
||||
var self = this;
|
||||
process.nextTick(function () {
|
||||
self.req.on('end', function () {
|
||||
self.res.end();
|
||||
})
|
||||
self.req.buffer = false;
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(9090);
|
||||
console.log('union with director running on 9090');
|
||||
|
30
node_modules/union/examples/simple/spdy.js
generated
vendored
Normal file
30
node_modules/union/examples/simple/spdy.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
// In order to run this example you need to
|
||||
// generate local ssl certificate
|
||||
var union = require('../../lib'),
|
||||
director = require('director');
|
||||
|
||||
var router = new director.http.Router();
|
||||
|
||||
var server = union.createServer({
|
||||
before: [
|
||||
function (req, res) {
|
||||
var found = router.dispatch(req, res);
|
||||
if (!found) {
|
||||
res.emit('next');
|
||||
}
|
||||
}
|
||||
],
|
||||
spdy :{
|
||||
key: './certs/privatekey.pem',
|
||||
cert: './certs/certificate.pem'
|
||||
}
|
||||
});
|
||||
|
||||
router.get(/foo/, function () {
|
||||
this.res.writeHead(200, { 'Content-Type': 'text/plain' })
|
||||
this.res.end('hello world\n');
|
||||
});
|
||||
|
||||
server.listen(9090, function () {
|
||||
console.log('union with director running on 9090 with SPDY');
|
||||
});
|
13
node_modules/union/examples/socketio/README
generated
vendored
Normal file
13
node_modules/union/examples/socketio/README
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
This folder contains an example of how to use Union with Socket.io.
|
||||
|
||||
First, you'll want to install both Union and Socket.io. Run this
|
||||
command in the folder you placed these two files:
|
||||
|
||||
npm install union socket.io
|
||||
|
||||
You can run the server like so:
|
||||
|
||||
node server.js
|
||||
|
||||
Now open up your web browser to http://localhost and see the results
|
||||
in the console!
|
8
node_modules/union/examples/socketio/index.html
generated
vendored
Normal file
8
node_modules/union/examples/socketio/index.html
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
var socket = io.connect('http://localhost');
|
||||
socket.on('news', function (data) {
|
||||
console.log(data);
|
||||
socket.emit('my other event', { my: 'data' });
|
||||
});
|
||||
</script>
|
30
node_modules/union/examples/socketio/server.js
generated
vendored
Normal file
30
node_modules/union/examples/socketio/server.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
var fs = require('fs'),
|
||||
union = require('union');
|
||||
|
||||
var server = union.createServer({
|
||||
before: [
|
||||
function (req, res) {
|
||||
fs.readFile(__dirname + '/index.html',
|
||||
function (err, data) {
|
||||
if (err) {
|
||||
res.writeHead(500);
|
||||
return res.end('Error loading index.html');
|
||||
}
|
||||
|
||||
res.writeHead(200);
|
||||
res.end(data);
|
||||
});
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
server.listen(9090);
|
||||
|
||||
var io = require('socket.io').listen(server);
|
||||
|
||||
io.sockets.on('connection', function (socket) {
|
||||
socket.emit('news', {hello: 'world'});
|
||||
socket.on('my other event', function (data) {
|
||||
console.log(data);
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user