{"__v":0,
"date":"2014-11-20T01:39:34.964Z",
"item":"drink sculpin",
"_id":"546d46569142b4887382f1c3"}
{"__v":0,
"date":"2014-11-20T01:39:34.964Z",
"item":"drink sculpin",
"_id":"546d46569142b4887382f1c3"}
{"__v":0,
"date":"2014-11-20T01:39:34.964Z",
"item":"drink sculpin",
"_id":"546d46569142b4887382f1c3"}
↓
↓
source: http://adambard.com/blog/top-github-languages-2014/
New repos on GitHub by language
Event driven vs multi-threaded web server
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World. I am running node.js\n');
}).listen(1337);
example.js
% node example.js
% brew install node
{
"name": "intro-to-mean",
"main": "server/server.js",
"version": "0.0.1",
"scripts": {
"start": "grunt"
},
"dependencies": {
"body-parser": "~1.9.2",
"connect": "~3.3.3",
"express": "~4.5.1",
"ejs": "~0.8.5",
"lodash-node" : "~2.4",
"mongoose": "~3.8.19",
},
"engines": {
"node": "~0.10.32"
}
}
% npm install
1) "Check node_modules into git for things you deploy, such as websites and apps. Do not check node_modules into git for libraries and modules intended to be reused. Use npm to manage dependencies in your dev environment, but not in your deployment scripts."
{
"name": "A",
"version": "0.1.0",
"dependencies": {
"B": {
"version": "0.0.1",
"dependencies": {
"C": {
"version": "0.1.0"
}
}
}
}
}
2) Use NPM Shrinkwrap
package.json
{
"name": "A",
"version": "0.1.0",
"dependencies": {
"B": "<0.1.0"
}
}
package B:
{
"name": "B",
"version": "0.0.1",
"dependencies": {
"C": "<0.1.0"
}
}
and package C:
{
"name": "C,
"version": "0.0.1"
}
→
"Fast, unopinionated, minimalist web framework for node."
require 'sinatra'
get '/hi' do
"Hello World!"
end
var express = require('express');
var app = express();
app.get('/hi', function (req, res) {
res.send('Hello World!');
})
var express = require('express'),
http = require('http');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World. I am running node.js/express.');
});
http.createServer(app);
http.listen(1337);
var app = express();
// a middleware with no mount path;
// gets executed for every request to the app
app.use(function (req, res, next) {
console.log('Time:', Date.now());
next();
});
// a middleware mounted on /user/:id; will be
// executed for any type of HTTP request to /user/:id
app.use('/user/:id', function (req, res, next) {
console.log('Request Type:', req.method);
next();
});
// a route and its handler function (middleware system)
// which handles GET requests to /user/:id
app.get('/user/:id', function (req, res, next) {
res.send('USER');
});
var express = require('express'),
bodyParser = require('body-parser'),
var app = express();
// load the body parsing middleware
app.use(bodyParser.json());
// my controller middleware
this.create = function(req, res){
// read in user request in http body
newTodo.item = req.body.item;
// save newTodo item
};
app.set('view engine', 'ejs');
app.get('/', function(req, res) {
res.render('index.ejs');
});
async.waterfall([
function(callback){
callback(null, 'one', 'two');
},
function(arg1, arg2, callback){
// arg1 now equals 'one' and arg2 now equals 'two'
callback(null, 'three');
},
function(arg1, callback){
// arg1 now equals 'three'
callback(null, 'done');
}
], function (err, result) {
// result now equals 'done'
});
https://github.com/caolan/async
% npm install async --save
var mongoose = require('mongoose');
var todoSchema = {
item : String,
date : Date
};
exports.model = function(mongoConnection){
return mongoConnection.model('Todo', todoSchema, 'todos');
};
library for creating data model/validation
exports: Node uses commonjs (http://wiki.commonjs.org/wiki/CommonJS)
model/index.js
var Todo = require('../models/').model(mongoConnection);
this.list = function(req, res){
Todo.find( { } , function(err, doc){
res.send(doc);
});
};
controller/index.js
GET /items
this.read = function(req, res){
Todo.find( { _id : req.params.idx },
function(err, doc){
res.send(doc);
}
);
};
GET /items/:idx
{"__v":0,
"date":"2014-11-20T01:39:34.964Z",
"item":"drink sculpin",
"_id":"546d46569142b4887382f1c3"}
{"__v":0,
"date":"2014-11-20T01:39:34.964Z",
"item":"drink sculpin",
"_id":"546d46569142b4887382f1c3"}
{"__v":0,
"date":"2014-11-20T01:39:34.964Z",
"item":"drink sculpin",
"_id":"546d46569142b4887382f1c3"}
↓
↓
this.create = function(req, res){
var newTodo = new Todo();
newTodo.item = req.body.item;
newTodo.date = new Date();
newTodo.save(function(err, doc) {
if(err)
throw err;
res.send(doc);
});
};
Ideally suited for Single Page Applications
<body ng-app='app'>
...
</body>
html
app.js
angular.module("app",[ ]);
app.controller("todoController", function($scope){
$scope.greeting = "Hello World. This is my todo app";
})
<body ng-app='app'>
<div ng-controller='todoController'>
<h1>{{greeting}}</h1>
</div>
</body>
html template
app.js
$http.get('http://localhost:3245/item')
.success(function(result){
$scope.todos = result;
});
$http.post('http://localhost:3245/item',
{item : todo.item})
.success(function(result){
console.log(result);
});
@edatrero
github.com/eatrero