Skip to content

Instantly share code, notes, and snippets.

@jfhbrook jfhbrook/example.js
Created Jul 16, 2012

Embed
What would you like to do?
Thinking about what I want from inline microtemplate action.
var pen = require('./pen');
var stdout = pen(process.stdout)
.$$ // Enter "templating" mode
('testing, testing,') // returns the function itself so you can chain these
('%d, %j', 1, {2: 3}) // Each call does a stream.write w/ util.format + '\n'
.$$ // Returns the stream again, so you could .end(), etc.
.write('and back again.\n')
;
var util = require('util');
module.exports = function (stream, delimiter) {
delimiter = delimiter || '$$';
var fxn = function () {
stream.write(util.format.apply(null, arguments) + '\n');
return fxn;
};
fxn[delimiter] = stream;
stream[delimiter] = fxn;
return stream;
};
#!/usr/bin/env node
var http = require('http'),
util = require('util'),
pen = require('./pen'),
memory,
error;
try {
memory = process.memoryUsage();
}
catch (e) {
error = e;
}
http.createServer(function (req, res) {
pen(res);
res.writeHead(200, {'Content-Type': 'text/html'});
res.$$
('<!doctype html>')
('<html>')
(' <head>')
(' <title>Does process.memoryUsage() Work?</title>')
(' </head>')
(' <body>')
(' <center>')
(' <h1>%s</h1>', !error ? 'YES' : 'NO')
;
if (error) {
res.$$
(' <pre>')
(error)
(' </pre>')
;
}
res.$$
(' </center>')
(' </body>')
('</html')
.$$.end();
}).listen(8080);
console.log('> http server has started on port 8080');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.