<script src="js/jquery.js"></script> <script src="js/jquery.lightbox.js"></script> <script src="js/effects.js"></script>
// src/helper.js var fs = require('fs'); module.exports = { saveTheQueen: function (name) { fs.writeFile(name+".txt", name+" was saved."); } } // src/main.js var helper = require('./helper'); helper.saveTheQueen("Elizabeth");
$ browserify src/main.js -o dist/bundle.js
// scripts/fooify.js define(function () { // ... }); // scripts/main.js require(['jquery', 'fooify'], function ($, fooify) { // ... });
<!-- index.html --> <script data-main="scripts/main" src="scripts/require.js"></script>
function forEach (list, fn) { for (i = 0; i < list.length; i++) { fn(list[i]); } } var user = ['Hans', 'Paul']; forEach(users, function (user) { console.log(user); });
var fs = require('fs'); fs.writeFileSync('example.txt', "Lorem Ipsum"); console.log("File written");
var fs = require('fs'); function log (err, res) { if (!err) { console.log("File written"); } } fs.writeFile('example1.txt', "One", log); fs.writeFile('example2.txt', "Two", log);
Verwendet jQuery.
var currentUser = {name: "Pascal"}; jQuery.getJSON('/me', function (result) { currentUser = result.data; });
async(param1, param2, function (err, result) {});
Genauer: Promises/A+ [9]
var currentUser = {name: "Pascal"}; jQuery.getJSON('/me') .then(function (result) { currentUser = result.data; }, function (err) { console.error(err); });
requests = readConfig() .then(function (config) { return Promise.all(urls, function (url) { return request(config, url); }); }) .then(userInput) .then(writeFile('result.txt')) .catch(function (error) { console.error(error); })
var Promise = require('bluebird'); var maybe = new Promise(function (resolve, reject) { var x = Math.random(); setTimeout(function () { if (x > 0.5) { resolve("yay"); } else { reject("nope"); } }, 1000); });
var count = 1; jQuery('button').on('click', function (event) { console.log('Clicked', count, 'times'); count = count + 1; });
var fs = require('fs'); var file = fs.createReadStream('test.csv'); file.on('data', logProgress); file.on('error', logError); file.on('end', logSuccess);
jQuery('#bar').on('click', function () { jQuery('#foo').trigger('magic', {answer: 42}); }); jQuery('#foo').on('magic', function (event, data) { console.log("Answer is", data.answer); });
var request = require("request"); var zlib = require("zlib"); var fs = require("fs"); // HTTP GET Request request("http://nodestreams.com/input/people.json.gz") // Un-Gzip .pipe(zlib.createGunzip()) // Write File .pipe(fs.createWriteStream("output/people.json"));
Vergleiche auch Beispiele auf nodestreams.com.
[1] S. Powers, Learning node. O’Reilly Media, Inc., 2012.
[2] Google, „V8 JavaScript Engine: Changelog“, 2008. https://code.google.com/p/v8/source/browse/trunk/ChangeLog. (Zugegriffen: 26. September 2014)
[3] D. Crockford, JavaScript: The Good Parts. O’Reilly Media, Inc., 2008.
[4] D. Ryan, „Node.js“, 2009. http://nodejs.org/. (Zugegriffen: 26. September 2014)
[5] CommonJS group, „CommonJS Modules 1.1.1“. 23. März 2010. http://wiki.commonjs.org/wiki/Modules/1.1.1. (Zugegriffen: 28. September 2014)
[6] Browserling Inc., „Browserify website“, 2012. http://browserify.org/. (Zugegriffen: 22. September 2014)
[7] T. Koppers, „webpack module bundler“, 4. März 2012. http://webpack.github.io/. (Zugegriffen: 21. September 2014)
[8] J. Burke, „Require.js“, 8. September 2014. http://requirejs.org/. (Zugegriffen: 18. Oktober 2014)
[9] B. Cavalier und D. Denicola, „Promises/A+“. 6. Dezember 2012. http://promisesaplus.com/. (Zugegriffen: 26. September 2014)