Module

Node unterstützt ein simples Modul System. Module werden einfach als Dateien geladen.

var mymod = require('./myMod.js');
console.log(mymod.init('node'));
exports.init = function(name) {
   return name;
};
var count = 0;
var privateMethod = function() {
    count++;
    return count;
};
var publicMethod = function() {
    privateMethod();
    return 'called ' + count;
}

module.exports = publicMethod;

Das Buffer Objekt steht global zur Verfügung.
__filename und __dirname stehen auch zur Verfügung.

Core Module
Die Core Module von Node sind im /lib Verzeichnis zu finden.

util Modul

var util = require('util');
util.debug('debug message and process ends');
util.log('timestamp message');

path Modul
Stellt Hilfsmittel zum arbeiten mit Datei Pfaden zur Verfügung.

var path = require('path');
path.normalize('/foo/bar//baz/asdf/quux/..') // '/foo/bar/baz/asdf'
path.dirname('/foo/bar/baz/asdf/quux') // '/foo/bar/baz/asdf'
path.basename('/foo/bar/baz/asdf/quux.html') // quux.html
path.basename('/foo/bar/baz/asdf/quux.html', '.html') // quux

url Modul
Das Modul stellt Hilfsmittel zur Auflösung und interpretieren von URLs zur Verfügung.

var url = require('url');
var parsedUrl = url.parse('/home?page=two');
parsedUrl = { href: '/home?page=two',
              search: '?page=two',
              query: 'page=two',
              pathname: '/home' };

var pathname = url.parse('/home?page=two').pathname;

Comments are closed.