Node.js

dirty

Key Value Database (npmjs.org)

var dirty = require('dirty');
var db = dirty('/store.db');
var data = db.get('foobar');
db.set('foobar', "some content");

request

HTTP Requests (npmjs.org)

var request = require('request');
request(url, function (error, response, html) {
  if (error || response.statusCode != 200) { console.error(error, response); return false;}
  console.log(html);
});

cheerio

jQuery like DOM Parser (npmjs.org)

var cheerio = require('cheerio');
var $ = cheerio.load(body);

underscore

Utiliys (npmjs.org)

var _ = require('underscore');

dialog

Window Prompts (npmjs.org)

var dialog = require('dialog');
dialog.info("text", "title");
 
 
 
===== commander =====
Commandline Parameter ([[https://www.npmjs.org/package/commander|npmjs.org]])
<code javascript>
var argv = require('commander');
 
argv
  .version('0.0.1')
  .option('-p, --peppers', 'Add peppers')
  .option('-c, --cheese [type]', 'Add type of cheese [marble]', 'marble')
  .parse(process.argv);
 
console.log('you ordered a pizza with:');
if (argv .peppers) console.log('  - peppers');
console.log('  - %s cheese', program.cheese);

prompt

Userinput Abfrage (npmjs.org)

var prompt = require('prompt');
prompt.get(['username', 'email'], function (err, result) {
  console.log('Command-line input received:');
  console.log('  username: ' + result.username);
  console.log('  email: ' + result.email);
});

fs-extra

Extra FileSystem tools (npmjs.org)

var fs = require('fs-extra')
 
// Async with promises:
fs.copy('/tmp/myfile', '/tmp/mynewfile')
  .then(() => console.log('success!'))
  .catch(err => console.error(err))
 
// Async with callbacks:
fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
  if (err) return console.error(err)
  console.log('success!')
})
 
// Sync:
try {
  fs.copySync('/tmp/myfile', '/tmp/mynewfile')
  console.log('success!')
} catch (err) {
  console.error(err)
}

mime

MIME-Type (npmjs.org)
https://www.npmjs.org/package/mime

var mime = require('mime');
 
mime.lookup('/path/to/file.txt');         // => 'text/plain'
mime.lookup('file.txt');                  // => 'text/plain'
mime.lookup('.TXT');                      // => 'text/plain'
mime.lookup('htm');                       // => 'text/html'

async

Queue, Warteschlange (npmjs.org)

var async = require('async');
var queue = async.queue(worker, 40);
queue.push(ip);
queue.drain = function() {
  console.log("all done");
}
 
function worker (host, callback) {
  console.log("Hard Working");
  for(var i = 0; i <= 5; i++) {
    if(i == 5) 7
      callback();
    }  
  }  
}

socket.io

Realtime Web Communication (npmjs.org)

server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 8080;
 
server.listen(port, function () {
  console.log('Server listening at port %d', port);
});
 
app.use(express.static(__dirname + '/public'));
 
io.on('connection', function (socket) {
  socket.on('say', function (data) {
    // send to current request socket client
    socket.emit('message', "this is a test");
 
    // sending to all clients, include sender
    io.sockets.emit('message', "this is a test");
 
    // sending to all clients except sender
    socket.broadcast.emit('message', "this is a test");
 
    // sending to all clients in 'game' room(channel) except sender
    socket.broadcast.to('game').emit('message', 'nice game');
 
    // sending to all clients in 'game' room(channel), include sender
    io.sockets.in('game').emit('message', 'cool game');
 
    // sending to individual socketid
    io.sockets.socket(socketid).emit('message', 'for your eyes only');
  });
});
client.html
<html>
  <head></head>
  <body>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io();
      socket.emit('say', 'Hello from: ' + prompt("Your Name?"));
      socket.on('message', function (data) {
        console.log(data);
      });      
    </script>
  </body>
</html>

mkdirp

(npmjs.org)

var mkdirp = require('mkdirp');
 
mkdirp('./tmp/foo/bar/baz', function (err) {
    if (err) console.error(err)
    else console.log('pow!')
});

validator

Eingabe Überprüfung (npmjs.org)

var validator = require('validator');
validator.isEmail('foo@bar.com'); //=> true

chalk

Farben/Colors in der Console (npmjs.org)

var chalk = require('chalk');
 
// Combine styled and normal strings 
log(chalk.blue('Hello') + 'World' + chalk.red('!'));
 
// Compose multiple styles using the chainable API 
log(chalk.blue.bgRed.bold('Hello world!'));
 
// Pass in multiple arguments 
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
 
var error = chalk.bold.red;
var warning = chalk.keyword('orange');
console.log(error('Error!'));
console.log(warning('Warning!'));
pub/node.js.txt · Zuletzt geändert: von Marco Krage