Building Node Applications with MongoDB and Backbone by Mike Wilson

Building Node Applications with MongoDB and Backbone by Mike Wilson

Author:Mike Wilson [Mike Wilson]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Web / Web Programming
ISBN: 9781449337759
Publisher: O'Reilly Media
Published: 2012-12-06T16:00:00+00:00


Node.js

The Express methods have expanded slightly during this chapter, supporting the new data that is sent to and from the account model. Because all of the display and processing is handled on the frontend by Backbone.js, Node’s involvement for this stretch has been relegated to the simple getting and setting of account data.

Aside from the GET route for the application data in Example 7-17, there are three new status-related routes in the updated application class. Both activity and status have GET operations that Backbone uses to populate its collection objects, but only status has a POST operation. This is because the application’s users are only able to post status updates; even when they create a status from the activity view, they are really performing the same action as if they added a status from their profile-only status list. When a status is saved to the database, it is Express’s job to ensure it is pushed out to all of the corresponding activity feeds.

Example 7-17. The updated app.js

var express = require("express"); var app = express(); var nodemailer = require('nodemailer'); var MemoryStore = require('connect').session.MemoryStore; var dbPath = 'mongodb://localhost/nodebackbone'; // Import the data layer var mongoose = require('mongoose'); var config = { mail: require('./config/mail') }; // Import the models var models = { Account: require('./models/Account')(config, mongoose, nodemailer) }; app.configure(function(){ app.set('view engine', 'jade'); app.use(express.static(__dirname + '/public')); app.use(express.limit('1mb')); app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({ secret: "SocialNet secret key", store: new MemoryStore() })); mongoose.connect(dbPath, function onMongooseError(err) { if (err) throw err; }); }); app.get('/', function(req, res){ res.render('index.jade'); }); app.post('/login', function(req, res) { console.log('login request'); var email = req.param('email', null); var password = req.param('password', null); if ( null == email || email.length < 1 || null == password || password.length < 1 ) { res.send(400); return; } models.Account.login(email, password, function(account) { if ( !account ) { res.send(401); return; } console.log('login was successful'); req.session.loggedIn = true; req.session.accountId = account._id; res.send(200); }); }); app.post('/register', function(req, res) { var firstName = req.param('firstName', ''); var lastName = req.param('lastName', ''); var email = req.param('email', null); var password = req.param('password', null); if ( null == email || email.length < 1 || null == password || password.length < 1 ) { res.send(400); return; } models.Account.register(email, password, firstName, lastName); res.send(200); }); app.get('/account/authenticated', function(req, res) { if ( req.session.loggedIn ) { res.send(200); } else { res.send(401); } }); app.get('/accounts/:id/activity', function(req, res) { var accountId = req.params.id == 'me' ? req.session.accountId : req.params.id; models.Account.findById(accountId, function(account) { res.send(account.activity); }); }); app.get('/accounts/:id/status', function(req, res) { var accountId = req.params.id == 'me' ? req.session.accountId : req.params.id; models.Account.findById(accountId, function(account) { res.send(account.status); }); }); app.post('/accounts/:id/status', function(req, res) { var accountId = req.params.id == 'me' ? req.session.accountId : req.params.id; models.Account.findById(accountId, function(account) { status = { name: account.name, status: req.param('status', '') }; account.status.push(status); // Push the status to all friends account.activity.push(status); account.save(function (err) { if (err) { console.log('Error saving account: ' + err); } }); }); res.send(200); }); app.get('/accounts/:id', function(req, res) { var accountId = req.params.id == 'me' ? req.session.accountId : req.params.id; models.Account.findById(accountId, function(account) { res.send(account); }); }); app.post('/forgotpassword', function(req, res) { var hostname = req.headers.host; var resetPasswordUrl = 'http://' + hostname + '/resetPassword'; var email = req.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.