http://www.js-attitude.fr/2014/09/22/libs-javascript-front/
top-10-mistakes-node-developers-make
grunt “The Javascript Task Runner”
Node Package Manager
Au 11/12/2016 le site npm propose plus de 350.000 packages !
Affichage de la liste des packages globaux
npm list -g –depth=0
Affichage de la liste des packages locaux
npm list –depth=0
express “Fast, unopinionated, minimalist web framework”
Framework de base pour un projet web.
Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. Perfect for development.
Voir aussi : forever
Magique: Permet au server de demander au client de recharger une page quand une vue change.
Pour cela il utilise inject-html
var app = express();
// .....
var reloadify = require('reloadify')([path.join(__dirname, 'views'), path.join(__dirname, 'less')]);
app.use(reloadify);
// plus loin, après le démarrage du server
// serve an empty page that just loads the browserify bundle
app.get('/', function(req, res) {
res.render('home');
});
debug Assez basique, mais efficace.
Dans un module :
var debug = require('debug')('mon-module');
.....
debug('myvar: '+myvar);
.....
Pour activer/désactiver le debug des modules depuis un shell:
export DEBUG='mon-module, mon-autre-module....'
pug (replace jade)
Template language exemple :
doctype html
html(lang="en")
head
title= pageTitle
script(type='text/javascript').
if (foo) bar(1 + 5)
body
h1 Pug - node template engine
#container.col
if youAreUsingPug
p You are amazing
else
p Get on it!
p.
Pug is a terse and simple templating language with a
strong focus on performance and powerful features.
mocha “Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.”
test à la récine du projet
//............
"scripts": {
"start": "node app",
"test": "mocha"
},
//............
var assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal(-1, [1,2,3].indexOf(4));
});
});
});
> npm test
chai “Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.”
sinon “Standalone test spies, stubs and mocks for JavaScript. No dependencies, works with any unit testing framework.”
istambul “a JS code coverage tool written in JS”
karma “Spectacular Test Runner for JavaScript. ” utilise istanbul
underscorejs origine: CoffeeScript et Backbone
Utilitaire général : A voir
Exemple:
var bunyan = require('bunyan');
global.log = bunyan.createLogger({
name: 'GaGaZ',
streams: [{
level: 'info',
type: 'rotating-file',
path: __dirname+'/log/app.log',
period: '1d', // daily rotation
count: 3 // keep 3 back copies
},
{
level: 'error',
type: 'rotating-file',
path: __dirname+'/log/app-errors.log',
period: '1d', // daily rotation
count: 3 // keep 3 back copies
}]
});
//.............................................
log.trace('hi on trace'); // Level 10
log.debug('hi on debug'); // Level 20
log.info('hi on info'); // Level 30
log.warn('hi on warn'); // Level 40
log.error('hi on error'); // Level 50
log.fatal('hi on fatal'); // Level 60
Browserify lets you require('modules') in the browser by bundling up all of your dependencies.
Divers outils qui permettent de mieux maitriser les problématiques asynchrones.
async Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript.
'use strict';
var async = require("async");
function makeLoopFunction(label, val) {
return function(callback) {
// console.log(label+val);
return callback(null, {'val': val, 'label': label});
};
}
function done (err, result) {
console.log('cbk done: '+result.length);
console.log(result);
}
var funcs = [];
for(var i = 0; i < 3; i++) {
funcs.push(makeLoopFunction('test 5: ', i));
}
async.parallel(funcs, done);
// cbk done: 3
// [ { val: 0, label: 'test 5: ' },
// { val: 1, label: 'test 5: ' },
// { val: 2, label: 'test 5: ' } ]
co generator async control flow goodness
bluebird est une implementation des 'promise' javascript.
'use strict';
var Promise = require('bluebird');
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
console.log('sleep: '+milliseconds);
return milliseconds;
}
}
}
Promise.all([sleep(30), sleep(60)])
.then(function(res){
console.log(res);
});
// sleep: 30
// sleep: 60
// [ 30, 60 ]
Permet d'exécuter des commandes systèmes.
https://medium.freecodecamp.org/node-js-child-processes-everything-you-need-to-know-e69498fe970a