Ultimate Express & Mongo Reference

Express

express()

// import express
const express = require("express")
// create an application object
const app = express()
// create new router
const router = express.Router()
// register middleware with the application, (each request will run through each of these one by one)
app.set("view engine", "ejs") // specify the view engine for view rendering
app.use(express.static("public")) // serve a folder called "public" as static
app.use(express.json()) // parse json bodies based on Content-Type header
app.use(express.urlencoded({extended: false})) // parse urlencoded form data
app.use("/", router) // register the router with the application
// register routes with the router
router.get("/path", getHandlerFunction) // invokes function for matching get request
router.post("/path", postHandlerFunction) // invokes function for matching post request
router.put("/path", putHandlerFunction) // invokes function for matching put request
router.delete("/path", deleteHandlerFunction) // invokes function for matching delete request
// Initiate the server listening on a port with a function that runs when server starts listening
app.listen(3000, () => console.log("Listening on Port 3000"))

The Request and Response Objects

Popular 3rd Party Middleware

Popular View Engines

Mongoose

Connecting to Mongo

const mongoose = require("mongoose")// connect to the database, 1st argument the connection string, 2nd argument a configuration object
mongoose.connect(MongoURI, ConfigObject)
//set responses to database events
mongoose.connection
.on("open", () => console.log("connected to mongo"))
.on("close", () => console.log("connected to mongo"))
.on("error", (error) => console.log(error))

Creating a Model Object

const {Schema, model} = require("mongoose")// create schema
const ModelSchema = new Schema({
property1: String,
property2: Number,
property3: Boolean
}, {timestamps: true})
// create model specifying model/collection name and schema
const Model = model("Model", ModelSchema)

3 Ways to Write Queries with your model with error handling

// Callback Syntax
Model.find({}, (error, data) => {
if (error){
console.log(error)
} else {
console.log(data)
}
})
// .then syntax
Model.find({})
.then((data) => console.log(data))
.catch((error) => console.log(error))
// using async/await
const queryFunction = async() => {
try{
const data = await Model.find({})
console.log(data)
} catch(error){ console.log(error) }
}
queryFunction()

Model Functions

Model.deleteMany()
Model.deleteOne()
Model.find()
Model.findById()
Model.findByIdAndDelete()
Model.findByIdAndRemove()
Model.findByIdAndUpdate()
Model.findOne()
Model.findOneAndDelete()
Model.findOneAndRemove()
Model.findOneAndReplace()
Model.findOneAndUpdate()
Model.replaceOne()
Model.updateMany()
Model.updateOne()

Further Reading

Alex Merced is a Full Stack Developer, learn more about his work at AlexMercedCoder.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store