Skip to content

Instantly share code, notes, and snippets.

@PaulMougel
PaulMougel / child.js
Created November 19, 2013 08:45
Parent-child communication using messages in Node.js
process.on('message', function(m) {
console.log('Just received a message from parent: ' + JSON.stringify(m));
if (m.exit === "true") process.exit();
});
@PaulMougel
PaulMougel / client.js
Last active May 1, 2024 12:38
File upload in Node.js to an Express server, using streams
// node: v0.10.21
// request: 2.27.0
var request = require('request');
var fs = require('fs');
var r = request.post("http://server.com:3000/");
// See http://nodejs.org/api/stream.html#stream_new_stream_readable_options
// for more information about the highWaterMark
// Basically, this will make the stream emit smaller chunks of data (ie. more precise upload state)
var upload = fs.createReadStream('f.jpg', { highWaterMark: 500 });
@PaulMougel
PaulMougel / sample-jsonstream.js
Created November 15, 2013 22:45
Sample stream usage in Node.js: parse a JSON array on-the-fly
var stream = require('stream');
var JSONStream = require('JSONStream');
var inputStream = new stream.PassThrough();
var jsonStream = JSONStream.parse('*');
var finalStream = new stream.Writable({ objectMode: true });
finalStream._write = function (doc, encoding, done) {
console.log(doc);
done();
};
@PaulMougel
PaulMougel / mime.js
Last active December 28, 2015 04:59 — forked from chmanie/gist:7441515
MIME checking using streams in Node.js
var stream = require('readable-stream');
var mmm = require('mmmagic');
var mimeChecker = new stream.Transform();
mimeChecker.data = [];
mimeChecker.mimeFound = false;
mimeChecker._transform = function (chunk, encoding, done) {
var self = this;
if (self.mimeFound) {
self.push(chunk);