tipical workflow:
git clean -f -d git pull origin/master npm install gulp build systemctl restart myapp
typings search node typings install --save --global dt~node typings install --save --global dt~mocha-node npm install -g mocha
The node.js event loop: node.js platform works in one single process which handles the web requests. But all the IO operations and the other function calls are asynchronous in nature. They are "waked" by a callback listener from the node.js main process and they run each of the calls in different threads (Child Processes). In the main process, the node.js server call is never blocked as this process only delegates the IO tasks to the sub processes (worker threads).
nodejs
#!/usr/bin/env node var fs = require('fs'); var qry = fs.existsSync('/etc/hosts'); var stat = fs.statSync('/etc/hosts'); var qry2 = stat.isFile(); fs.readFile(fs.realpathSync(filename), 'utf8', function (err,data) { if (err) { return console.log(err); } data; }); fs.writeFile(filename, data, function (err) { if (err) return console.log(err); console.log('File writing done'); });
simple node program:
#!/usr/bin/env node var s_date_time = new Date(); console.log('Stating: ' + s_date_time); var i_counter = 0; for (var $i_loop1 = 0; $i_loop1 < 10; $i_loop1++) { for (var $i_loop2 = 0; $i_loop2 < 32000; $i_loop2++) { for (var $i_loop3 = 0; $i_loop3 < 32000; $i_loop3++) { i_counter++; if (i_counter > 50) { i_counter = 0; } } } } var s_date_time_end = new Date(); console.log('End: ' + s_date_time_end + '\n');
CLI arguments
node nodecmd_test one two three
#!/usr/bin/env node process.argv.forEach(function (val, index, array) { console.log(index + ': ' + val); });
#!/usr/bin/env node process.stdin.resume(); process.stdin.setEncoding('utf8'); var util = require('util'); process.stdin.on('data', function (text) { console.log('received data:', util.inspect(text)); if (text === 'exit\\n') { complete(); } else { invalidCommand(); } }); function complete() { console.log('There is nothing to do now.'); process.exit(); } function invalidCommand() { console.log('Please enter the valid command'); }
var http = require('http');
// loading user defined modules
var checkData = require('./validatorutil');
var checkEmail = function(value) { try { check(value).isEmail(); } catch (e) { return e.message; //Invalid integer } return value; }; module.exports.checkEmail = checkEmail;
download modules:
npm install <>
simple node web server:
#!/usr/bin/env node var http = require("http"); var url = require("url"); function start_server(param){ function onRequest(request, response) { response.writeHead(200, {"Content-type": "text/html"}); response.write("<a>Smile!</a>"); response.end(); } http.createServer(onRequest).listen(8181, "127.0.0.1"); } start_server(param);
var buffer = new Buffer('this is a good place to start');
var slice = new Buffer(10);
var startTarget = 0,start = 10,end = 20;
buffer.copy(slice, startTarget, start, end);
console.log(buf.toString());
console.log(buf.toString('base64'));
buffer.fill("0");
var json = JSON.stringify(buf);
var util = require('util');
util.inherits(myEventEmitterClass, EventEmitter);
myEventEmitterClass.prototype.emittedMethod = function() {
console.log('before the emittedMethod');
this.emit('emittedevent');
console.log('after the emittedMethod');
}
someObject.on('emittedevent', function() {
console.log('The emittedevent is called');
});