On Github fheemeyer / hacking-node
"Node.js is simply a library written for V8 which does evented I/O"
$ echo "console.log('Hello World');" > foo.js $ node foo.js Hello World
$ echo "puts 'Hello World'" > foo.rb $ ruby foo.rb Hello World
// Reading files in node.js var fs = require('fs') , argument = process.argv[2]; var contents = fs.readFileSync(argument, 'utf-8'); console.log(contents);
$ echo 'foo' > data.txt $ node foo.js data.txt foo
/* * simple inheritance in node.js */ var util = require('util'); // ClassA definition var ClassA = function() { this.a = 'a'; }; // ClassB definition, inherits ClassA var ClassB = function() { ClassA.apply(this, arguments); this.b = 'b'; }; util.inherits(ClassA, ClassB);
$ npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sane defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (foo)
{ "name": "foo", "version": "0.0.1", "description": "Lorem ipsum dolor sit amet", "main": "index.js", "dependencies": { "MD5": "~1.2.0" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Finn-Lennart Heemeyer", "license": "ISC" }
$ npm install npm WARN package.json finn@0.0.0 No description npm WARN package.json finn@0.0.0 No repository field. npm WARN package.json finn@0.0.0 No README data npm http GET https://registry.npmjs.org/MD5 npm http 304 https://registry.npmjs.org/MD5 npm http GET https://registry.npmjs.org/charenc npm http GET https://registry.npmjs.org/crypt npm http 304 https://registry.npmjs.org/crypt npm http 304 https://registry.npmjs.org/charenc MD5@1.2.0 node_modules/MD5 ├── charenc@0.0.1 └── crypt@0.0.1 $
$.ajax({ method: 'get', url: 'http://www.google.de' }).then(function() { // Asynchronous console.log('Second!'); }); console.log('First!');
fs = require('fs'); fs.readFile('/doesnt/exist', 'utf8', function (err,data) { // Asynchronous if (err) { return console.log(err); } console.log(data); console.log('Second!'); }); console.log('First!');
function archiveOrders(date, cb) { db.connect(function(err, conn) { if (err) return cb(err); conn.query("select * from orders where date < ?", [date], function(err, orders) { if (err) return cb(err); helper.each(orders, function(order, next) { conn.execute("insert into archivedOrders ...", [order.id, ...], function(err) { if (err) return cb(err); conn.execute("delete from orders where id=?", [order.id], function(err) { if (err) return cb(err); next(); }); }); }, function() { console.log("orders have been archived"); cb(); }); }); }); }
// streamline.js function archiveOrders(date, _) { var conn = db.connect(_); conn.query("select * from orders where date < ?", [date], _).forEach_(_, function(_, order) { conn.execute("insert into archivedOrders ...", [order.id, ...], _); conn.execute("delete from orders where id=?", [order.id], _); }); console.log("orders have been archived"); }
var myModule = require('lib/my-own-module'); myModule.myModuleFunction('Hi'); var a = myModule.myModuleVariable;
var module = { myModuleFunction: function(str) { console.log(str); }, myModuleVariable: 1337 }; module.exports = module;
var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('hello world'); }); app.listen(3000);