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
Building Node Applications with MongoDB and Backbone by Mike Wilson.pdf
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.
ActiveX | ASP.NET |
Cold Fusion | CSS |
DHTML | Java Server Pages |
JavaScript | PHP |
Python | Ruby |
XSL |
Hello! Python by Anthony Briggs(9904)
The Mikado Method by Ola Ellnestam Daniel Brolund(9769)
Dependency Injection in .NET by Mark Seemann(9329)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7771)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7550)
Svelte with Test-Driven Development by Daniel Irvine(7088)
Test-Driven Development with PHP 8 by Rainier Sarabia(6818)
Layered Design for Ruby on Rails Applications by Dementyev Vladimir;(6684)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6530)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6399)
Web Development with Django by Ben Shaw Saurabh Badhwar(6150)
React Application Architecture for Production by Alan Alickovic(5871)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5804)
Kotlin in Action by Dmitry Jemerov(5048)
Audition by Ryu Murakami(4583)
Software Architecture for Web Developers by Mihaela Roxana Ghidersa(4412)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4313)
Accelerating Server-Side Development with Fastify by Manuel Spigolon Maksim Sinik & Matteo Collina(4265)
Functional Programming in JavaScript by Mantyla Dan(4037)
