2. HOW WE’RE BUILDING WERCKER
node.js essential modules and tools
Micha Hernández van Leuffen Wouter Mooij
@mies @wwwouter
mies@wercker.com wouter@wercker.com
Sunday, April 1, 12
4. wercker
Wercker is a hosted continuous deployment platform,
helping developer-teams working on web-applications,
push smaller pieces of code, such that bugs are quickly
detected and resolved.
It is easy to use and with you from the start.
Sunday, April 1, 12
7. EXPRESS
‣ Powers the Wercker API
‣ Similar to Sinatra in Ruby or Flask in
Python
‣ Just Use It
‣ expressjs.com
Sunday, April 1, 12
8. EXPRESS
// load dependencies
var express = require(‘express’);
var app = express.createServer();
// define routes, send JSON
app.get(‘/posts’, function(req, res) {
res.send({title: ‘this is a post’});
});
// use templating
app.get(‘/posts/:id’, function(req, res) {
res.render(‘post’, {title: ‘this is a post’});
});
Sunday, April 1, 12
9. EXPRESS Pro T
ip!
// separate controller from routes in app.js
var api = require(‘./controllers/api.js’);
app.get(‘/posts’, api.getPosts);
// have functionality reside in controllers/api.js
exports getPosts = function(req, res) {
res.send({title: ‘this is a post’});
});
Sunday, April 1, 12
10. ASYNC Esca
Callb ping
ack
Hell™
// separate controller from routes in app.js
fs.stat('/awesome/file', function (err, stats) {
parseData(stats, function(err, data) {
db.insert(data, function(err, result) {
// do something with error or result
});
});
Sunday, April 1, 12
11. ASYNC
‣ Control flow library (waterfall, series,
parallel)
‣ Functional Programming methods
(filter, map, reduce, forEach)
‣ Alternatives include (Step, Flow-js,
node-promise)
‣ https://github.com/caolan/async
Sunday, April 1, 12
12. ASYNC
async.waterfall([
function(callback){
fs.stat('/awesome/file', callback)
},
function(stats, callback){
parseData(stats, callback);
},
function(data, callback){
db.insert(json, callback);
}
],
// optional final callback to run once all functions have completed
function (err, result) {
// if there is an error it ends up here
// otherwise this is the result of the db insert
console.log(result);
});
Sunday, April 1, 12
15. MONGOOSE
‣ MongoDB = Document Oriented
Database
‣ Mongoose = MongoDB ODM
‣ Easy to define and query models
‣ http://mongoosejs.com/
Sunday, April 1, 12
16. MONGOOSE
// define a model
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var projectSchema = new Schema({
name: String,
git_url: String,
user_id: {type: ObjectId, required: true, index: true},
private: {type: Boolean, default: true},
createdOn: {type: Date, default: Date.now},
});
module.exports = mongoose.model('Project', projectSchema);
Sunday, April 1, 12
17. MONGOOSE
// use a model
var Project = require(‘project.js’);
// Find objects
Project.find({ user_id: id}, [‘name’,‘createdOn’], {sort: {'createdOn': -1}},
function(err, projects) {
// do something with array of projects
});
// Retrieve a single object
Project.findOne({ name: ‘mies’}, function(err, project) {
// do something with the project
});
// Create new Project
p = new Project({name: ‘mies’...}).save();
Sunday, April 1, 12