A simple introduction to the Node.Js envirnoment with a list of useful tool and resources.
...from nodejs.org
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
In the following "hello world" example, many connections can be handled concurrently. Upon each connection the callback is fired, but if there is no work to be done Node is sleeping.
...from npmjs.com
# For example Using Debian, as root
$ curl -sL https://deb.nodesource.com/setup_0.12 | bash -
$ apt-get install -y nodejs
$ sudo npm install npm -g
# Now run
$ npm -v.
# The version should be higher than 2.1.8.
Node comes with npm installed so you should have a version of npm. However, npm gets updated more frequently than Node does, so you'll want to make sure it's the latest version
# To install a global package you can use:
$ npm install --global <package name>
$ npm install -g <package name>
# If you have permission problem try with sudo:
$ sudo npm install -g <package name>
$ sudo npm install --global <package name>
# install the pageres command line tool
$ npm install --global pageres-cli
# the tool now is magically available as a command
$ pageres google.it 1024x768
$ npm install cfonts -g
$ npm install asciify -g
$ npm install chalk
// ~/myscript/index.js
var chalk = require('chalk');
// style a string
var st = chalk.blue('Hello world!');
console.log(st);
$ npm init
$ npm install chalk --save
$ npm install --global is-up
$ is-up --help
Example
is-up sindresorhus.com
✔︎ Up
$ npm install --save is-up
var isUp = require('is-up');
isUp('sindresorhus.com', function (err, up) {
console.log(up);
//=> true
});
$ npm install http-server -g
// usage
$ http-server [path] [options]
Commander is a complete solution for node.js command-line programs
#!/usr/bin/env node
/**
* Module dependencies.
*/
var program = require('commander');
program
.version('0.0.1')
.option('-p, --peppers', 'Add peppers')
.option('-P, --pineapple', 'Add pineapple')
.option('-b, --bbq-sauce', 'Add bbq sauce')
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
.parse(process.argv);
console.log('you ordered a pizza with:');
if (program.peppers) console.log(' - peppers');
if (program.pineapple) console.log(' - pineapple');
if (program.bbqSauce) console.log(' - bbq');
console.log(' - %s cheese', program.cheese);
var agenda = new Agenda({db: { address: 'localhost:27017/agenda-example'}});
agenda.define('delete old users', function(job, done) {
User.remove({lastLogIn: { $lt: twoDaysAgo }}, done);
});
agenda.every('3 minutes', 'delete old users');
agenda.schedule('tomorrow at noon', 'printAnalyticsReport', {userCount: 100});
agenda.every('15 minutes', 'printAnalyticsReport');
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'sender@gmail.com',
pass: 'password'
}
});
transporter.sendMail({
from: 'sender@address',
to: 'receiver@address',
subject: 'hello',
text: 'hello world!'
});