Be MEAN
Criando aplicações com MongoDb, Express,
AngularJs e Node.js
github.com/suissa
about.me/suissa
@osuissa
C++
JSON/BSON
Replica
Sharding
GridFS
Map/Reduce
V8
C/C++
I/O Async
Event Drive
MVC
Routes
Templates
Directives
Dependency Injection
Two Way Data-binding
Organização dos arquivos
app.js
package.json
modules/
db/
public/
routes/
views/
package.json
{
"name": "application-name"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "3.0.0"
, ...
app.js
/**
* Module dependencies.
*/
var express = require('express'),
routes = require('./routes'),
api = require('./rout...
app.js
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
...
app.js
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true,
showStack: true }));
...
app.js
// Routes
app.get('/', routes.index);
app.get('/user/:id, routes.user);
// JSON API
app.get('/api/user/:id, api.use...
app.js
// Start server
app.listen(3000, function(){
console.log("Server rodando na porta 3000");
});
routes/api.js
/**
* Create user
*/
exports.userPost = function (req, res) {
var user = new User(req.body)
user.save(functi...
routes/api.js
exports.userGet = function (req, res, next, id) {
User
.findOne({ _id : id })
.exec(function (err, user) {
i...
public/js/myapp.js
'use strict';
angular.module('myApp', ['myApp.filters', 'myApp.
services', 'myApp.directives']).
config...
public/js/controllers.js
'use strict';
/* Controllers */
function AppCtrl($scope, $http, $routeParams) {
var id = $routePa...
modules/db/user.js
/**
* Module dependencies.
*/
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
mongoose.c...
modules/db/user.js
/**
* User Schema
*/
var UserSchema = new Schema({
name: { type: String, default: '' },
email: { type: ...
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Be MEAN
Upcoming SlideShare
Loading in …5
×

Be MEAN

3,458 views

Published on

Construindo sistemas com MongoDb, Express, AngularJs e Node.js. Palestra ministrada no TDC São Paulo 2013. Trilha Javascript.

Published in: Technology

Be MEAN

  1. 1. Be MEAN Criando aplicações com MongoDb, Express, AngularJs e Node.js
  2. 2. github.com/suissa about.me/suissa @osuissa
  3. 3. C++ JSON/BSON Replica Sharding GridFS Map/Reduce
  4. 4. V8 C/C++ I/O Async Event Drive
  5. 5. MVC Routes Templates Directives Dependency Injection Two Way Data-binding
  6. 6. Organização dos arquivos app.js package.json modules/ db/ public/ routes/ views/
  7. 7. package.json { "name": "application-name" , "version": "0.0.1" , "private": true , "dependencies": { "express": "3.0.0" , "jade": ">= 0.0.1" , "mongo": "*" , "mongoose": "*" } }
  8. 8. app.js /** * Module dependencies. */ var express = require('express'), routes = require('./routes'), api = require('./routes/api'); var app = module.exports = express();
  9. 9. app.js // Configuration app.configure(function(){ app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.static(__dirname + '/public')); app.use(app.router); });
  10. 10. app.js app.configure('development', function(){ app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.configure('production', function(){ app.use(express.errorHandler()); });
  11. 11. app.js // Routes app.get('/', routes.index); app.get('/user/:id, routes.user); // JSON API app.get('/api/user/:id, api.userGet); app.post('/api/user, api.userPost); app.put('/api/user, api.userPut); app.delete('/api/user, api.userDelete); // redireciona todas outras para o index app.get('*', routes.index);
  12. 12. app.js // Start server app.listen(3000, function(){ console.log("Server rodando na porta 3000"); });
  13. 13. routes/api.js /** * Create user */ exports.userPost = function (req, res) { var user = new User(req.body) user.save(function (err) { if (err) { return res.render('users/signup', { errors: utils.errors(err.errors), user: user, title: 'Sign up' }) } }
  14. 14. routes/api.js exports.userGet = function (req, res, next, id) { User .findOne({ _id : id }) .exec(function (err, user) { if (err) return next(err); else return user; }) }
  15. 15. public/js/myapp.js 'use strict'; angular.module('myApp', ['myApp.filters', 'myApp. services', 'myApp.directives']). config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { $routeProvider.when('/user, {templateUrl: 'user', controller: AppCtrl}); $routeProvider.when('/user/:id, {templateUrl: 'user', controller: AppCtrl}); $routeProvider.otherwise({redirectTo: '/}); $locationProvider.html5Mode(true); }]);
  16. 16. public/js/controllers.js 'use strict'; /* Controllers */ function AppCtrl($scope, $http, $routeParams) { var id = $routeParams.id; $http({method: 'GET', url: '/api/user/'+id}). success(function(data, status) { $scope.name = data.name; $scope.msg = 'Usuário encontrado.'; }). error(function(data, status { $scope.msg = 'Usuário não encontrado!'; }); }
  17. 17. modules/db/user.js /** * Module dependencies. */ var mongoose = require('mongoose') , Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/database_name); var db = mongoose.connection;
  18. 18. modules/db/user.js /** * User Schema */ var UserSchema = new Schema({ name: { type: String, default: '' }, email: { type: String, default: '', unique: true }, username: { type: String, default: '' }, hashed_password: { type: String, default: '' }, salt: { type: String, default: '' }, authToken: { type: String, default: '' }, }); mongoose.model('User', UserSchema);

×