heinzelmann

A loose collection of useful and useless Utilities

npm install heinzelmann
14 downloads in the last week
23 downloads in the last month

node-heinzelmann

The Node-Heinzelmann is a loose collection of useful and useless Utilities written in JavaScript for node.js.

For Installation type npm install heinzelmann

README Contents

http-response

Utilities to write a response to the client. You get it with:

require('heinzelmann').util('http-response', args);

download

Send a File to the client.

Without options:

var http = require('http');
var heinzelmann = require('heinzelmann');

var server = http.createServer(function(req, res) {
  var resUtil = heinzelmann.util('http-response', res);
  resUtil.download('/tmp/98we72w9a34.zip');
});

server.listen(3000);

With options:

var http = require('http');
var heinzelmann = require('heinzelmann');

var server = http.createServer(function(req, res) {
  var resUtil = heinzelmann.util('http-response', res);
  var options = {
    'clientname': 'your-download.zip' // file presented to client as "your-download.zip"
  }; 
  resUtil.download('/tmp/98we72w9a34.zip', options);
});

server.listen(3000);

json

Answer request with json.

var http = require('http');
var heinzelmann = require('heinzelmann');

var server = http.createServer(function(req, res) {
  var resUtil = heinzelmann.util('http-response', res);
  var thing = {
    'foo': 'bar'
  };
  resUtil.json(thing);
});

server.listen(3000);

xml

Answer request with an old lady (without any validation).

var http = require('http');
var heinzelmann = require('heinzelmann');

var server = http.createServer(function(req, res) {
  var resUtil = heinzelmann.util('http-response', res);
  var thing = '<foo></foo>';
  resUtil.xml(thing);
});

server.listen(3000);

http-request

Grep interesting information out of user's request.

ip

return the ip address of the client. try to get the address even in case of proxies.

var http = require('http');
var heinzelmann = require('heinzelmann');

var server = http.createServer(function(req, res) {
  console.log(heinzelmann.util('http-request', request).ip());
});

server.listen(3000);

summary

return a summary of the request with ip address, cookies, user-agent and the date now. this is useful if you are not interested in the entire request paramesters.

var http = require('http');
var heinzelmann = require('heinzelmann');

var server = http.createServer(function(req, res) {
  console.log(heinzelmann.util('http-request', request).summary());
});

server.listen(3000);

mongo-factory

produce mongo purposes

client

return a mongoclient setting default values. this is useful if you are tired to type "127.0.0.1" ...

var mongodb = require('mongodb');
var heinzelmann = require('heinzelmann');
var mongoclient = heinzelmann.util('mongo-factory', 'db_name').client();

sitemap-factory

produce a google sitemap out of a given object. use given default values or system default values (lastmod "now", priority 1.00 and changefreq "monthly").

get

return a google xml sitemap. example (with own priority and changefreq as default, use system lastmod)

var heinzelmann = require('heinzelmann');
var smf = heinzelmann.util('sitemap-factory', 'http://foo.bar');
var urlset = [ {
  loc : '/'
}, {
  loc : '/loc-1.html',
  lastmod : new Date(2000, 0, 1, 2, 3, 4)
}, {
  loc : 'loc-2.html',
  lastmod : '2011-12-16T10:36:26+00:00',
  priority : 0.4,
  changefreq : 'never'
} ];
var defaults = {
  priority : 0.8,
  changefreq : 'daily'
};

The output (where 2012-07-11T10:13:28+00:00 is the date "now"):

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
  <url>
    <loc>http://foo.bar</loc>
    <lastmod>2012-07-11T10:13:28+00:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.80</priority>
  </url>
  <url>
    <loc>http://foo.bar/loc-1.html</loc>
    <lastmod>2000-01-01T02:03:04+00:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.80</priority>
  </url>
  <url>
    <loc>http://foo.bar/loc-2.html</loc>
    <lastmod>2011-12-16T10:36:26+00:00</lastmod>
    <changefreq>never</changefreq>
    <priority>0.40</priority>
  </url>
</urlset>

common-date-format

format dates to common used representations.

for other things see:

https://github.com/felixge/node-dateformat

https://github.com/minodisk/dateformat-js

for more options see file doc.

getRow

return the date in a row

var heinzelmann = require('heinzelmann');
var date = new Date(2003, 4, 6, 7, 12, 55);
var cdf = heinzelmann.util('common-date-format', date);
console.log(cdf.getRow())

output:

'20030506071255'

getW3CDateTime

return a date in W3C Date and Time format with complete date plus hours, minutes and seconds:

http://www.w3.org/TR/NOTE-datetime

var heinzelmann = require('heinzelmann');
var date = new Date(2003, 4, 6, 7, 12, 55);
var cdf = heinzelmann.util('common-date-format', date);
console.log(cdf.getW3CDateTime())

output:

'2003-05-06T07:12:55+00:00'
npm loves you