Skip to content

Commit

Permalink
update ch7 text to new ES.Next and versions
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-co committed Nov 12, 2017
1 parent 669abbf commit 35840c1
Show file tree
Hide file tree
Showing 7 changed files with 883 additions and 708 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Expand Up @@ -5,5 +5,6 @@
"mongoskin",
"truthy",
"truthyness"
]
],
"cSpell.language": "en,en-US"
}
1,536 changes: 850 additions & 686 deletions chapter7/chapter7.md

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions code/ch7/blog-mongoose/app.js
@@ -1,5 +1,5 @@
var TWITTER_CONSUMER_KEY = process.env.TWITTER_CONSUMER_KEY || 'ABC'
var TWITTER_CONSUMER_SECRET = process.env.TWITTER_CONSUMER_SECRET || 'XYZXYZ'
const TWITTER_CONSUMER_KEY = process.env.TWITTER_CONSUMER_KEY || 'ABC'
const TWITTER_CONSUMER_SECRET = process.env.TWITTER_CONSUMER_SECRET || 'XYZXYZ'

const express = require('express')
const routes = require('./routes')
Expand Down Expand Up @@ -82,7 +82,11 @@ app.use((req, res, next) => {

// Authorization
const authorize = function (req, res, next) {
if (req.session && req.session.admin) { return next() } else { return res.send(401) }
if (req.session && req.session.admin) {
return next()
} else {
return res.send(401)
}
}

// Pages and routes
Expand Down
21 changes: 13 additions & 8 deletions code/ch7/blog-mongoose/models/article.js
@@ -1,10 +1,12 @@
var mongoose = require('mongoose');
const mongoose = require('mongoose')

var articleSchema = new mongoose.Schema({
const articleSchema = new mongoose.Schema({
title: {
type: String,
required: true,
validate: [function(value) {return value.length<=120}, 'Title is too long (120 max)'],
validate: [function (value) {
return value.length <= 120
}, 'Title is too long (120 max)'],
default: 'New Post'
},
text: String,
Expand All @@ -14,13 +16,16 @@ var articleSchema = new mongoose.Schema({
},
slug: {
type: String,
set: function(value){return value.toLowerCase().replace(' ', '-')}
set: function (value) {
return value.toLowerCase().replace(' ', '-')
}
}
});
})

articleSchema.static({
list: function(callback){
this.find({}, null, {sort: {_id:-1}}, callback);
list: function (callback) {
this.find({}, null, {sort: {_id: -1}}, callback)
}
})
module.exports = mongoose.model('Article', articleSchema);

module.exports = mongoose.model('Article', articleSchema)
4 changes: 2 additions & 2 deletions code/ch7/blog-mongoose/models/index.js
@@ -1,2 +1,2 @@
exports.Article = require('./article');
exports.User = require('./user');
exports.Article = require('./article')
exports.User = require('./user')
15 changes: 8 additions & 7 deletions code/ch7/blog-mongoose/models/user.js
@@ -1,13 +1,14 @@
var mongoose = require('mongoose');
const mongoose = require('mongoose')

var userSchema = new mongoose.Schema({
const userSchema = new mongoose.Schema({
email: {
type: String,
required: true,
set: function(value) {return value.trim().toLowerCase()},
set: function (value) { return value.trim().toLowerCase() },
validate: [
function(email) {
return (email.match(/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i) != null)},
function (email) {
return (email.match(/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i) != null)
},
'Invalid email'
]
},
Expand All @@ -16,6 +17,6 @@ var userSchema = new mongoose.Schema({
type: Boolean,
default: false
}
});
})

module.exports = mongoose.model('User', userSchema);
module.exports = mongoose.model('User', userSchema)
2 changes: 1 addition & 1 deletion code/ch7/mongoose-example/mongoose.js
Expand Up @@ -4,7 +4,7 @@ mongoose.Promise = global.Promise
const Book = mongoose.model('Book', { name: String })

const practicalNodeBook = new Book({ name: 'Practical Node.js' })
practicalNodeBook.save(function (err, results) {
practicalNodeBook.save((err, results) => {
if (err) {
console.error(err)
process.exit(1)
Expand Down

0 comments on commit 35840c1

Please sign in to comment.