https://github.com/vmlf01/nodejs-presentation
the motive behind node.js
node architecture
the event loop
npm & CommonJS modules
LIVE CODING: a simple API server
unit testing
must see:
Try it out:
console.log('Hello World');
$ node index.js
setTimeout(function () {
console.log('Hello World');
}, 5000);
$ node index.js
or
split hello world into 2 files:
// hello.js
module.exports.sayHello = function (name) {
console.log('Hello ' + name);
};
// index.js
var hello = require('./hello');
hello.sayHello('World');
$ node index.js
create a service that will tell:
$ npm init
$ npm install
$ npm install -g <package>
$ npm install --save <package>
$ npm install --save-dev <package>
$ npm start
$ npm test
$ npm run <script>
let's use npm init to create our project
most important attributes are:
let's create a folder structure like this:
"Ada improves code safety and maintainability by using the compiler
to find errors in favor of runtime errors."
and yet...
$ npm install mocha --save-dev
$ npm install should --save-dev
require('should');
var primes = require('../src/primes.js');
describe('primes', function () {
it('should export a member function isPrime');
it('should export a member function countPrimes');
describe('isPrime', function () {
it('should return false if N is less than 2');
it('should return true if N is only divisible by 1 and itself');
});
describe('countPrimes', function () {
it('should return 0 if N is less than 2');
it('should return number of primes between 1 and N inclusive');
});
});
basic HTTP server
// require node 'http' core module
var http = require('http');
// read port from environment variable 'PORT' or use '3000' as default
var port = process.env.PORT || 3000;
// create an http server and set the request handler
var server = http.createServer(function (req, res) {
res.writeHead(200, { "Content-Type": "application/json" });
res.write(JSON.stringify({ msg: 'Hello', requestPath: req.url }));
res.end();
});
// start up server on all IP interfaces on specified port
server.listen(port, '0.0.0.0', function () {
console.log('App server is running on http://0.0.0.0:' + port);
});
however...
remember the mantra
let's use express
$ npm install express --save
quick express overview
incoming requests go through a series of steps
that process the request and can terminate
or hand off the request to the next step in the pipeline
// require express module
var express = require('express');
// create a new express 'application'
var app = express();
// add middleware to the pipeline
app.use(function (req, res, next) {
console.log('Incoming request for ' + req.url);
next();
});
// add a route handler
app.get('*', function (req, res) {
res.send('Hello World!');
});
// start server listening on port 3000
app.listen(3000, function () {
console.log('Example app listening on http://localhost:3000');
});
to test our API endpoints, we are going to use
Super-agent driven library for testing
node.js HTTP servers using a fluent API
var request = require('supertest');
var app = require('../src/app');
describe('primes api', function () {
describe('/isprime', function () {
it('should return 200 if request is valid');
it('should return 400 if request is invalid');
it('should return result object in body');
});
describe('/count', function () {
it('should return 200 if request is valid');
it('should return 400 if request is invalid');
it('should return result object in body');
});
});
API basic routes tests
let's create our API pipeline
// require express module
var express = require('express');
// require our primes module
var primes = require('./primes');
// create a new express 'application'
var app = express();
// add middleware to the pipeline
app.use(function (req, res, next) {
console.log('Incoming request for ' + req.url);
next();
});
// add route handlers
app.get('/isprime/:number', handleIsPrime);
app.get('/count/:number', handlePrimeCount);
// export the express app
module.exports = app;
and use our primes module to handle requests
// require our primes module
var primes = require('./primes');
function handleIsPrime(req, res) {
var number = Number(req.params.number);
if (isNaN(number)) {
res.status(400).end();
}
else {
res.status(200).send({ result: primes.isPrime(number) });
}
}
function handlePrimeCount(req, res) {
var number = Number(req.params.number);
if (isNaN(number)) {
res.status(400).end();
}
else {
res.status(200).send({ result: primes.countPrimes(number) });
}
}
# install istanbul package
$ npm install --save-dev istanbul
# on linux we can just use "_mocha"
$ istanbul --include-all-sources cover _mocha
# on windows, we need to specify the path to the "mocha" script
$ istanbul --include-all-sources cover node_modules/mocha/bin/_mocha
SINFO also organizes some NodeSchool
events once in a while