Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
2,254
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
44
Comments
1
Likes
5

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Be MEAN Criando aplicações com MongoDb, Express, AngularJs e Node.js
  • 2. github.com/suissa about.me/suissa @osuissa
  • 3. C++ JSON/BSON Replica Sharding GridFS Map/Reduce
  • 4. V8 C/C++ I/O Async Event Drive
  • 5. MVC Routes Templates Directives Dependency Injection Two Way Data-binding
  • 6. Organização dos arquivos app.js package.json modules/ db/ public/ routes/ views/
  • 7. package.json { "name": "application-name" , "version": "0.0.1" , "private": true , "dependencies": { "express": "3.0.0" , "jade": ">= 0.0.1" , "mongo": "*" , "mongoose": "*" } }
  • 8. app.js /** * Module dependencies. */ var express = require('express'), routes = require('./routes'), api = require('./routes/api'); var app = module.exports = express();
  • 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. app.js app.configure('development', function(){ app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.configure('production', function(){ app.use(express.errorHandler()); });
  • 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. app.js // Start server app.listen(3000, function(){ console.log("Server rodando na porta 3000"); });
  • 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. 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. 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. 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. modules/db/user.js /** * Module dependencies. */ var mongoose = require('mongoose') , Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/database_name); var db = mongoose.connection;
  • 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);