This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const display = (res, items) => { | |
let lines = []; | |
items.forEach(item => { | |
lines.push("<hr>"); | |
lines.push([`${item.name}: $${item.price}`]); | |
}); | |
lines = lines.join("<br>"); | |
res.writeHead(200, { "Content-Type": "text/html" }); | |
res.end(lines); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const opts = { | |
most: () => [{}, {}, { sort: { price: -1 } }], | |
least: (most) => [{ category: most.category }, {}, { sort: { price: 1 } }], | |
sum: (most, least) => { | |
return [ | |
[ | |
{ | |
$match: { | |
category: most.category, | |
_id: { $nin: [most._id, least._id] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get("/callback", (req, res) => { | |
const errCall = e => { | |
res.status(500).end(e.stack); | |
}; | |
Product.findOne(...mostOpts(), (err, most) => { | |
if (err) return errCall(err); | |
Product.findOne(...leastOpts(most), (err, least) => { | |
if (err) return errCall(err); | |
Product.aggregate(...sumOpts(most, least), (err, sum) => { | |
if (err) return errCall(err); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Product.findOne(...mostOpts()) | |
.then(most => { | |
Product.findOne(...leastOpts(most)) | |
.then(least => { //... }) | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Product.findOne(...mostOpts()) | |
.then(most => { | |
return Promise.all([ most, Product.findOne(...leastOpts(most)) ]); | |
}) | |
.spread(most, least => { //... }) | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get("/promise", (req, res) => { | |
let most; | |
let least; | |
Product.findOne(...mostOpts()) | |
.then(m => { | |
most = m; | |
return Product.findOne(...leastOpts(most)); | |
}) | |
.then(l => { | |
least = l; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get("/async", async (req, res) => { | |
try { | |
const most = await Product.findOne(...mostOpts()); | |
const least = await Product.findOne(...leastOpts(most)); | |
const sum = await Product.aggregate(...sumOpts(most, least)); | |
display(res, [most, least, sum[0]]); | |
} catch (e) { | |
res.status(500).end(e.stack); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// non-sequential actions | |
const promises = [ Product.find(), Category.find() ] | |
// handling with plain promises | |
Promise.all(promises) | |
.spread((products, categories) => { | |
doLiterallyAnything(products, categories) | |
}); | |
// handling with await |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require("mongoose"); | |
const Schema = mongoose.Schema; | |
const uniqueValidator = require("mongoose-unique-validator"); | |
const bcrypt = require("bcrypt"); | |
const UserSchema = new Schema({ | |
username: { type: String, required: true, unique: true }, | |
passwordHash: { type: String, required: true } | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const passport = require("passport"); | |
app.use(passport.initialize()); | |
app.use(passport.session()); |