0% found this document useful (0 votes)
134 views34 pages

Back End

The document discusses various concepts related to backend engineering. It covers communication protocols, web servers, databases, caching, messaging systems, API frameworks, security practices, and more. It also discusses front-end and backend architectures, including how client-side requests are handled by the backend. Key backend technologies mentioned include Node.js, Express.js, and serving static resources.

Uploaded by

Albano Futta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views34 pages

Back End

The document discusses various concepts related to backend engineering. It covers communication protocols, web servers, databases, caching, messaging systems, API frameworks, security practices, and more. It also discusses front-end and backend architectures, including how client-side requests are handled by the backend. Key backend technologies mentioned include Node.js, Express.js, and serving static resources.

Uploaded by

Albano Futta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

BACKEND ENGINEERING

*Communication protocols (TCP, UDP, HTTP, Websocket, RFC)

*Web servers

*Database engineering

*Proxies (reverse proxies, caching layers, load balancers, TLS Termination)

*Caching (stateful, stateless)

*Messaging systems (Rabit & Queue, Cafca)

*API Web frameworks (Nodejs, Jango, Express, REST APIs)

*Message formats (XML, JSON, Javascript, protocol buffer)

*Security (encryption, TLS, denial service attack)

Front-end Arquitecture

Client side makes resquests, and back-end gives response to those requests to the client side.

Scripts (process requests),

Back-end Arquitecture

Server, server-side software (scripts and frameworks), database, API.

Stack = (framework, database, server, OS)

APIs = (how data is exchanged between a database and any app accessing it)

Back-end can be: cloud-based server and data warehouses,

Containerization with a service like Docker,

Back-end-as-a-Service (BaaS) providers,

APIs to replace more complex processing.

Middleware (APIs)
Let client-server talk and provides services (like data integration and error handling). Also maximize IT
efficiency and power things (like user engagement, business process management, content
management, authentication, etc.)

1 learn basic coding skill

2 master data structures and algorithms

3 learn the basics of the web

What is a web server?

What is HTTP?

1 Why HTTP

2 DNS

3 URIs

4 Async

5 Errors

6 Headers, Body

7 JSON

8 Methods (GET, PUT, POST, DELETE - RUCD)

9 Paths

10 HTTPS

How webpages communicate with servers?

4 Get started building simple web servers

Learn back-end techs and concepts like:

REST

Websockets

GraphQL

5 learn database fundamentals

MySQL

Redis
6 Deploy some projects to your professional portfolio
Node.js
Server environment, uses js on the server.

*Generates dynamic page content

*create, open, read, write, delete and close files on the server

*add, delete, modify data in db

Node.js files (.js)


*contains tasks that’ll be executed after certain events

*event, someone trying to access a port on the server

Initiate the Node.js file

*node filename.js  to run the Node.js file on the server

Buffer  data type, to store binary data.

Export modules

module.exports = { codes… }  to export a module and make it global

exports.fName  to export a prop of an object(file) then require it.

Install Packages

npm install <packageName> - -save  to include dependency in the package.json of the app

npm install <pN> -g  to include packages globally, independent of the current path.

Modules
Libraries, a set of functions to include in the app

Built-in Modules

Can be used without instalations.

Include Modules

Require(“mod_name”)
Require(“./mod_name”)  to include own module, exported from a file

HTTP Module
 Allows to transfer data over Hyper Text Transfer Protocol. require(“http”)

Creates an HTTP server that, listens to server ports and, gives response back to client

Web Server

.createServer(function).listen(8080)  creates a server object that, listens on port 8080. Practically web
server is to access web pages of an app. It returns Request(url, header, data, etc.) and Response objects

Add an HTTP Header

.writeHead(200, {‘Content-Type’: ’text/html’})  200, all’s ok. {} object containing the response header.

.write(“”)  response body. String to be sent to the request

.end()  to finally send the response.

JSON.stringify(objectHere)  in write() to serve JSON response.

Var http = require(‘http’);

http.createServer(function(req, res){

res.writeHead(200, {‘Content-Type’: ’text/html’})

})

Read the query string

Req  represents the request from the client, as an object

.url  prop which holds the part of the url that comes after the domain name

Split the Query String (URL Module)

to turn it into readable parts.

Var url = require(“url”);

http.createServer(function(req, res){

res.writeHead(200, {‘Content-Type’: ’text/html’})

var q = url.parse(req.url, true).query;

var txt = q.year + “ ” + q.month;

res.end(txt);

}).listen(8080)
File System Module
allows to work with the file system on the computer. Read, create, update, delete and rename files.

Include

Require(‘fs’)

Read files

.readFile(‘fileName’, function)

.readFileSync(‘fileName’, ‘utf8’)  to read the file synchronously, returns the file data

Var http = require(‘http’);

Var fs = require(“fs”);

http.createServer(function(req, res){

fs.readFile(‘demo.html’, function(err, data){

res.writeHead(200, {‘Content-Type’: ’text/html’})

res.write(data);

return res.end();

});

}).listen(8080)

Create Files

*.appendFile(‘fileNme’, ‘content’, function)  appends specified content to a file, case it doesn’t exist
then it’s auto created.

Var fs = require(‘fs’);

Fs.appendFile(‘mynewfile1.txt’, ‘Hello content!’, function(err){

If (err) throw err; console.log(‘Saved!’);

})

*.writeFile()  like the prior but, replaces the specified content case it exists. Case it doesn’t, a new file
with the specified content is auto created.
*.open(‘fileName’, ‘flag’, function) takes a flag, ‘w’ or ‘r’, case it doesn’t exist an empty file’s created.

Var fs = require(‘fs’);

Fs.open(‘mynewfile1.txt’, ‘w’, function(err, file){

If (err) throw err; console.log(‘Saved!’);

})

Update Files

*.appendFile()  appends the content to the end of the file

*.writeFile() replaces the content and the file

Delete Files

*.unlink(‘fileName’, function) 

Event Loop
runs in a single-threaded event-driven model. Observers executes once the event is fired.

Event module  var events = require(‘events’). To get the EventEmitter() reference.

EventEmitter()  events class. eventEm = new events.eventEmitter(). Used as JS-addEventListener()

Methods

.on(‘eventName’, eventHandler()) / addListener()  to handle an event

.once()  happens the next time it’s called then, gets removed.

.removeListener()

.removeAllListeners([eventName])  removes all listeners, or those specified.

.setMaxListeners(nº) / .getMaxListeners()

.listeners(event) returns a copy of array listeners of that event

.listenerCount(type)  returns the numb of listeners that type/event has

.emit(‘eventName’ [,par1,par2…])  to create an event


Common Events Patterns

*return EventEmitter from a function.

*extend the EventEmitter class.

Blocking I/o

Happpens when: reading a file, querying a database, socket request.

Debugging
With core debugger, node inspector, IDE built-in debugger.

Core debugger

*debugger  used on code where to stop the code.

*next  used on prompt, to run the next statement

*cont  as next, to continue & stop only at next debugger (if any).

*watch(‘exp’) / watchers  as next, to add values into watch / to see the values at watch.

*pause  to pause code running

Node inspector

Npm install –g node-inspector  to install it. Is a GUI based.

Run it

node-inspector  to run it.

node-inspector –web-port=5500  to set it to another port just for debugging purposes.

Start debugging

Node –debug-br appName.js  to start debugging.


Express.js (Node framework)
Install
npm i –g express

Express.js web App


Provides an easy way to create web server n render HTML pages forfor different HTTp requests by
configuring routes for ur app.

Web server

Import Express.js module and create the web server

Var exp = require(“express”);

Var app = exp(); // first define routes here…

Var server = app.listen(5000, ftion(){console.log(‘Node server’)})

Configure routes

Above app object has get(), post(), put() n delete() methods to define routes 4 HTTP GET, POST, PUT n
DELETE requests.

Var express = require(‘express’);

Var app = express();

App.get(‘/’, ftion(req, res){

Res.send(‘<html><body><h1>Hello World</h1></body></html>’)

})  ‘/’ the path of a route dat’ll start after base URL. Ftion callback wit request n response objects dat’ll
run on each get resquest.

App.post(‘/submit-data’, ftion(req, res){

Res.send(‘POST Request’)

})
App.put(‘/update-data’, ftion(req, res){

Res.send(‘PUT Request’)

})

App.delete(‘/delete-data’, ftion(req, res){

Res.send(‘DELETE Request’)

})

Var server = app.listen(5000, ftion(){console.log(‘Node server running’)})

Handle POST Request

Npm install body-parser  the middleware to handle HTTP POST resquest. It parses the JSON, buffer, str
n url encoded data submitted.

Var exp = require (‘express’);

Var app = app();

Var bodyParser = require(‘body-parser’);

App.use(bodyParser.urlencoded(extended : false));  use() mounts a certain fction for each request.

App.get(‘/’, ftion(req, res){

Res.sendFile(‘formFile.html’)

});

App.post(‘/submit-student-data’, ftion(req, res){

Var name = req.body.inputName+’ ’+req.body.inputName2;

Res.send(name + ‘Submitted!’);

})

Var server = app.listen(5000, ftion(){console.log(‘Node server running’)})


Serving Static Resources in Node.js
we’ll use Express.js n node-static module to serve static resources (like imgs, css, Javascript, etc.)

Serve Static Resources using Express.js (express.static)

Express.static()  u just have to specify the folder name where u’ve stored ur static resources.

Var express = require (‘express’);

Var app = express();

//setting middle ware

App.use(express.static(_ _dirname + ‘Public’)); // Serves resources from public folder. Use() mounts the
middleware express.static for every request.

App.use( ‘/images’, express.static(_ _dirname + ‘Images’));  use() mounts for evy request that start wit
‘/images’. Serves all the request which includes /images in the url from Images folder, can also create a
virtual path.

Var server = app.listen(5000)

Serve Static Resources using Node-static Module

The node-static module is an HTTP static-file server module wit built-in caching.

Npm install node-static

Var http = require(‘http’);

Var nStatic = require(‘node-static’);

Var fileserver = new nStatic.Server(‘./public’);

http.createServer(ftion(req, res){

fileserver.serve(req, res);

}).listen(5000);
Data Access in Node.js
Node.js supports all kinds of databases. However NoSQL dbs best fit wit it.

Npm i mysql to access a db from Node.js u first need to install its driver.

Npm i mongodb  for mongodb driver

Access SQL Server in Node.js

Npm i mssql, to access relational db MSSQL Server 2012 in Node.js app using Express.js

Var express = require(‘express’);

Var app = express();

App.get(‘/’, ftion(req, res)){

Var sql = require(‘mssql’);

//config for ur database

Var config = {user:’sa’, password;’mypass’, server:’localhost’, database:’SchollDB’};

//connect to ur database

Sql.connect(config, ftion(err){

if(err) console.log(err);

//create Request object

Var request = new sql.Request();

//query to the database n get the records

Request.query(‘select * from Srudent’, ftion(err, recordset){

If(err) console.log(err) //send records as a response

Res.send(recordset);

})

})

Var server = app.listen(5000, ftion(){console.log(‘Node server running’)});


Access MongoDB in Node.js
Npm i mongodb - -save  to access document-based database MongoDB using Node.js

Connecting MongoDB

Var MongoClient = require(‘mongodb’).MongoClient;

//connect to the db

MongoClient.connect(“mongodb://localhost:27017/MyDB”, ftion(err, db){

If(err) throw err; //write db Insert/Update/Query code here… Using db parameter

})  if the db path doesn’t exist then, it’s created.

Inserting Documents

Var MongoClient = require(‘mongodb’).MongoClient;

MongoClient.connect(“mongodb://localhost:27017/MyDB”, ftion(err, db){

db.collection(‘Persons’, ftion(err, collection){

collection.insert({id:1, firstName:’Steve’, lastName:’Jobs’});

collection.insert({id:2, firstName:’Bill’, lastName:’Gates’});

db.collection(‘Persons’).count(ftion(err, count)){

if(err) throw err;

console.log(“Total Rows: “+count)

})

})
Update/Dalete Documents

Var MongoClient = require(‘mongodb’).MongoClient;

MongoClient.connect(“mongodb://localhost:27017/MyDB”, ftion(err, db){

db.collection(‘Persons’, ftion(err, collection){

collection.updare({id:1},{$set: { firstName:’James’, lastName:’Gosling’}}, {w:1}, ftion(err,


result){

if(err) throw err; console.log(Document Updated Successfully)

});

collection.remove({id:2}, {w:1}, ftion(err, result){

if(err) throw err; console.log(Document Removed Successfully)

});

})

Query Database

Var MongoClient = require(‘mongodb’).MongoClient;

MongoClient.connect(“mongodb://localhost:27017/MyDB”, ftion(err, db){

db.collection(‘Persons’, ftion(err, collection){

collection.find().toArray(ftion(err, items){

if(err) throw err; console.log(items);

});

Res.json({})  to send data to a request

.env file is a hidden file that is used to pass environment variables to your


application
The environment variables are accessible from the app
as process.env.VAR_NAME=value.  to set or get its value.
Npm I dotenv  to use it.
require('dotenv').config() 

Root level middleware


Middleware functions are functions that take 3 arguments: the request object,
the response object, and the next function in the application’s request-
response cycle.
They can also end the cycle by sending a response when some condition is
met
If they don’t send the response when they are done, they start the execution
of the next function in the stack. This triggers calling the 3rd argument, next()
function(req, res, next) {
console.log("I'm a middleware...");
next();
}

Req.method, req.path, req.ip  to get the http method, path n caller’s ip of a request that reaches a
route.

Middlewares can be chained within a route definition:

app.get('/user', function(req, res, next) {


req.user = getTheUserSync(); // Hypothetical synchronous
operation
next();
}, function(req, res) {
res.send(req.user);
}); 
This approach is useful to split the server operations into smaller units. That
leads to a better app structure, and the possibility to reuse code in different
places. This approach can also be used to perform some validation on the
data. At each point of the middleware stack you can block the execution of the
current chain and pass control to functions specifically designed to handle
errors. Or you can pass control to the next matching route, to handle special
cases

Route parameters

req.params.name

sth/:name/eco

Query parameters
route_path: '/library'
actual_request_URL: '/library?userId=546&bookId=6754'
req.query: {userId: '546', bookId: '6754'}

app.route(path).get(handler).post(handler). This syntax allows you to


chain different verb handlers on the same path route. You can save a bit of
typing, and have cleaner code.

Done(null, result) or done(err)  for Node.js convention


MongoDB is a database application that stores JSON documents (or records) that you
can use in your application. Unlike SQL, another type of database, MongoDB is a non-
relational or "NoSQL" database. This means MongoDB stores all associated data within
one record, instead of storing it across many preset tables as in a SQL database.

Mongoose is a popular npm package for interacting with MongoDB. With Mongoose,
you can use plain JavaScript objects instead of JSON, which makes it easier to work
with MongoDB. Also, it allows you to create blueprints for your documents called
schemas, so you don't accidentally save the wrong type of data and cause bugs later.

Let mongoose = require(“mongoose”);

mongoose.connect(<Your URI>, { useNewUrlParser: true,


useUnifiedTopology: true });  to connect to the database

schema  maps to a mongodb collection. Defines shape of documents in dat collection. Building blocks
for Models

Collection  is equivalent to SQL’s table. Can store many JSON docs.

Document  instances of your objects


Model  allows you to create documents

Mongoose for MongoDB


Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It manages relationships
between data, provides schema validation, and is used to translate between objects in code and the
representation of those objects in MongoDB.

Collection  is equivalent to SQL’s table. Can store many JSON docs.

Documents  equivalent to SQL’s row

Fields 

Schema  While Mongo is schema-less, SQL defines a schema via the table definition. A Mongoose
‘schema’ is a document data structure (or shape of the document) that is enforced via the application
layer.
Model  are higher-order constructors that take a schema and create an instance of a document
equivalent to records in a relational database.

npm install mongoose validator  Let’s install Mongoose and a validation library with the following
command

./src/database.js

let mongoose = require('mongoose');

const server = '127.0.0.1:27017'; // REPLACE WITH YOUR DB SERVER


const database = 'fcc-Mail'; // REPLACE WITH YOUR DB NAME

class Database {
constructor() {
this._connect()
}

_connect() {
mongoose.connect(`mongodb://${server}/${database}`)
.then(() => {
console.log('Database connection successful')
})
.catch(err => {
console.error('Database connection error')
})
}
}

module.exports = new Database()

 a simple class with a method that connects to the database. Your connection string will vary based on
your installation.
A Mongoose model provides an interface to the database for creating, querying, updating, deleting
records, etc.

Creating a Mongoose model comprises primarily of three parts:

1. Referencing Mongoose

let mongoose = require('mongoose')

This reference will be the same as the one that was returned when we connected to the database,
which means the schema and model definitions will not need to explicitly connect to the database.

2. Defining the Schema

A schema defines document properties through an object where the key name corresponds to the
property name in the collection.

let emailSchema = new mongoose.Schema({

email: String

})

Here we define a property called email with a schema type String which maps to an internal validator
that will be triggered when the model is saved to the database. It will fail if the data type of the value is
not a string type.

The following Schema Types are permitted:

Array, Boolean, Buffer, Date, Mixed (A generic / flexible data type), Number, ObjectId, String.

Mixed and ObjectId are defined under require(‘mongoose’).Schema.Types.

3. Exporting a Model

We need to call the model constructor on the Mongoose instance and pass it the name of the collection
and a reference to the schema definition.

module.exports = mongoose.model('Email', emailSchema)

Let’s combine the above code into ./src/models/email.js to


define the contents of a basic email model:
let mongoose = require('mongoose')
let emailSchema = new mongoose.Schema({
email: String
})

module.exports = mongoose.model('Email', emailSchema)

A schema definition should be simple, but its complexity is


usually based on application requirements. Schemas can be
reused and they can contain several child-schemas too. In the
example above, the value of the email property is a simple
value type. However, it can also be an object type with
additional properties on it.

We can create an instance of the model we defined above and


populate it using the following syntax:

let EmailModel = require('./email')

let msg = new EmailModel({


email: '[email protected]'
})

Let’s enhance the Email schema to make the email property a


unique, required field and convert the value to lowercase before
saving it. We can also add a validation function that will ensure
that the value is a valid email address. We will reference and
use the validator library installed earlier.

let mongoose = require('mongoose')


let validator = require('validator')

let emailSchema = new mongoose.Schema({


email: {
type: String,
required: true,
unique: true,
lowercase: true,
validate: (value) => {
return validator.isEmail(value)
}
}
})

module.exports = mongoose.model('Email', emailSchema)

Basic Operations
Mongoose has a flexible API and provides many ways to
accomplish a task. We will not focus on the variations because
that is out of scope for this article, but remember that most of
the operations can be done in more than one way either
syntactically or via the application architecture.

Create Record
Let’s create an instance of the email model and save it to the
database:

let EmailModel = require('./email')

let msg = new EmailModel({


email: '[email protected]'
})

msg.save()
.then(doc => {
console.log(doc)
})
.catch(err => {
console.error(err)
})

Fetch Record
Let’s try to retrieve the record we saved to the database earlier.
The model class exposes several static and instance methods
to perform operations on the database. We will now try to find
the record that we created previously using the find method and
pass the email as the search term.

EmailModel
.find({
email: '[email protected]' // search query
})
.then(doc => {
console.log(doc)
})
.catch(err => {
console.error(err)
})

Update Record
Let’s modify the record above by changing the email address
and adding another field to it, all in a single operation. For
performance reasons, Mongoose won’t return the updated
document so we need to pass an additional parameter to ask
for it:

EmailModel
.findOneAndUpdate(
{
email: '[email protected]' // search query
},
{
email: '[email protected]' // field:values to update
},
{
new: true, // return updated doc
runValidators: true // validate before update
})
.then(doc => {
console.log(doc)
})
.catch(err => {
console.error(err)
})

Delete Record
We will use the findOneAndRemove call to delete a record. It
returns the original document that was removed:
EmailModel
.findOneAndRemove({
email: '[email protected]'
})
.then(response => {
console.log(response)
})
.catch(err => {
console.error(err)
})

Helpers
We have looked at some of the basic functionality above known
as CRUD (Create, Read, Update, Delete) operations, but
Mongoose also provides the ability to configure several types of
helper methods and properties. These can be used to further
simplify working with data.

Let’s create a user schema in ./src/models/user.js with the


fields firstName and lastName:
let mongoose = require('mongoose')

let userSchema = new mongoose.Schema({


firstName: String,
lastName: String
})

module.exports = mongoose.model('User', userSchema)

Virtual Property
A virtual property is not persisted to the database. We can add
it to our schema as a helper to get and set values.

Let’s create a virtual property called fullName which can be


used to set values on firstName and lastName and retrieve them
as a combined value when read:
userSchema.virtual('fullName').get(function() {
return this.firstName + ' ' + this.lastName
})

userSchema.virtual('fullName').set(function(name) {
let str = name.split(' ')

this.firstName = str[0]
this.lastName = str[1]
})
Callbacks for get and set must use the function keyword as we
need to access the model via the this keyword. Using fat arrow
functions will change what this refers to.
Now, we can set firstName and lastName by assigning a value
to fullName:
let model = new UserModel()

model.fullName = 'Thomas Anderson'

console.log(model.toJSON()) // Output model fields as JSON


console.log()
console.log(model.fullName) // Output the full name

Instance Methods
We can create custom helper methods on the schema and
access them via the model instance. These methods will have
access to the model object and they can be used quite
creatively. For instance, we could create a method to find all the
people who have the same first name as the current instance.

In this example, let’s create a function to return the initials for


the current user. Let’s add a custom helper method
called getInitials to the schema:
userSchema.methods.getInitials = function() {
return this.firstName[0] + this.lastName[0]
}
This method will be accessible via a model instance:
let model = new UserModel({
firstName: 'Thomas',
lastName: 'Anderson'
})

let initials = model.getInitials()

console.log(initials) // This will output: TA

Static Methods
Similar to instance methods, we can create static methods on
the schema. Let’s create a method to retrieve all users in the
database:

userSchema.statics.getUsers = function() {
return new Promise((resolve, reject) => {
this.find((err, docs) => {
if(err) {
console.error(err)
return reject(err)
}

resolve(docs)
})
})
}
Calling getUsers on the Model class will return all the users in
the database:
UserModel.getUsers()
.then(docs => {
console.log(docs)
})
.catch(err => {
console.error(err)
})
Adding instance and static methods is a nice approach to
implement an interface to database interactions on collections
and records.

Middleware
Middleware are functions that run at specific stages of a
pipeline. Mongoose supports middleware for the following
operations:

 Aggregate
 Document
 Model
 Query
For instance, models have pre and post functions that take two
parameters:
1. Type of event (‘init’, ‘validate’, ‘save’, ‘remove’)
2. A callback that is executed with this referencing the model instance

Let’s try an example by adding two fields


called createdAt and updatedAt to our schema:
let mongoose = require('mongoose')

let userSchema = new mongoose.Schema({


firstName: String,
lastName: String,
createdAt: Date,
updatedAt: Date
})

module.exports = mongoose.model('User', userSchema)


When model.save() is called, there is a pre(‘save’,
…) and post(‘save’, …) event that is triggered. For the second
parameter, you can pass a function that is called when the
event is triggered. These functions take a parameter to the next
function in the middleware chain.
Let’s add a pre-save hook and set values
for createdAt and updatedAt:
userSchema.pre('save', function (next) {
let now = Date.now()

this.updatedAt = now
// Set a value for createdAt only if it is null
if (!this.createdAt) {
this.createdAt = now
}

// Call the next function in the pre-save chain


next()
})
Let’s create and save our model:

let UserModel = require('./user')

let model = new UserModel({


fullName: 'Thomas Anderson'
}

msg.save()
.then(doc => {
console.log(doc)
})
.catch(err => {
console.error(err)
})

Plugins
Suppose that we want to track when a record was created and
last updated on every collection in our database. Instead of
repeating the above process, we can create a plugin and apply
it to every schema.

Let’s create a file ./src/model/plugins/timestamp.js and


replicate the above functionality as a reusable module:
module.exports = function timestamp(schema) {

// Add the two fields to the schema


schema.add({
createdAt: Date,
updatedAt: Date
})

// Create a pre-save hook


schema.pre('save', function (next) {
let now = Date.now()

this.updatedAt = now
// Set a value for createdAt only if it is null
if (!this.createdAt) {
this.createdAt = now
}
// Call the next function in the pre-save chain
next()
})
}
To use this plugin, we simply pass it to the schemas that should
be given this functionality:

let timestampPlugin = require('./plugins/timestamp')

emailSchema.plugin(timestampPlugin)
userSchema.plugin(timestampPlugin)

Query Building
Mongoose has a very rich API that handles many complex
operations supported by MongoDB. Consider a query where we
can incrementally build query components.

In this example, we are going to:

1. Find all users


2. Skip the first 100 records
3. Limit the results to 10 records
4. Sort the results by the firstName field
5. Select the firstName
6. Execute that query
UserModel.find() // find all users
.skip(100) // skip the first 100 items
.limit(10) // limit to 10 items
.sort({firstName: 1} // sort ascending by firstName
.select({firstName: true} // select firstName only
.exec() // execute the query
.then(docs => {
console.log(docs)
})
.catch(err => {
console.error(err)
})

Model.create([ {}, {}, {} ], (err, data)=>{ done(null, data)})  create many docs at once

Model.find(JSON object, callback)  to find for a query

Model.findOne() behaves like Model.find(), but it returns only one


document (not an array), even if there are multiple items. It is especially useful
when searching by properties that you have declared as unique.

Model.findById(id, callback)  find a doc by its id.

Model.update({to find}, {the edit}, callback)  Running Find, Edit, then


Save It is bound to the low-level mongo driver. It can bulk-edit many
documents matching certain criteria, but it doesn’t send back the updated
document, only a 'status' message. Furthermore, it makes model validations
difficult, because it just directly calls the mongo driver.
document.markModified('edited-field')  if you defined a doc in
Schema and you want its type to be changed, avoiding errors b4 the op u
wanna realize.

findOneAndUpdate() 

findByIdAndUpdate()  can be used when searching by id.

{ new: true } as the 3rd argument to return the updated doc.

findByIdAndRemove and findOneAndRemove are like the previous update


methods. They pass the removed document to the db

Model.remove() is useful to delete all the documents matching given criteria.

Chain Search Query Helpers to Narrow Search


Results
If you don’t pass the callback as the last argument to Model.find() (or to the
other search methods), the query is not executed. You can store the query in
a variable for later use. This kind of object enables you to build up a query
using chaining syntax. The actual db search is executed when you finally
chain the method .exec(). You always need to pass your callback to this last
method. There are many query helpers, here we'll use the most commonly
used.
Chain .find(), .sort(), .limit(), .select(), and then .exec(). Pass
the done(err, data) callback to exec().
Person.find({ age: 55 })

.sort({ name: -1 })

.limit(5)

.select({ favoriteFoods: 0 })

.exec(function(error, people) {
//do something here

});
App.all()  special routing method called to any HTTP method.

App.all(“/secret”, ( req, res, next ){})

Const express = require(“express”);

Const router = express.Router();

//Home page route

router.get(“/”, …)

//About page route

router.get(“/”, …)

module.exports = router;  Route handlers for all Wiki of the site in Wiki.js. made with express.Router.

&

Const wiki = require(“./wiki.js”);

// …

App.use(“/wiki”, wiki)  to use it in our main app file, we used use() to add the Router to the
middleware handling path.

You might also like