Framework built on top of Node. Allows us to quickly build out our server.
const express = require('express')
const app = express()
app.listen(3333, () => console.log('server running on port 3333'))
Express
Custom built behavior that allow us to dictate what is sent back or manage data that is sent to us.
app.get('/api/users', (req, res) => {
res.stats(200).send(users)
})
Above we have a GET request that will execute a callback function anytime a request is made to the '/api/users/' endpoint and send back all users.
Routes/Endpoint
const massive = require('massive')
massive(CONNECTION_STRING).then((database) => {
app.set('db', database)
})
app.get('/api/users', (req, res) => {
req.app.get('db').SQL_COMMAND().then((users) => {
res.send(users)
})
})
Massive connects to a Heroku hosted database. We then set a key/value pair called 'db' with a value of database on app. Anytime we want to run a sql command, Massive looks to the root of our project directory for a folder called db. We cannot call this anything else. It will then look for a corresponding sql command that it will execute as a function.
require('dotenv').config()
massive(process.env.CONNECTION_STRING)
const PORT = process.env.SERVER_PORT
dotenv always follows the process.env.VARIABLE_NAME syntax. Remember, we solemnly swear we are up to no good will never push up our .env file. The .env file should be at our project root, just like the db folder Massive looks for.
const express = require('express')
const app = express()
app.use(express.json())
Above we are telling our app variable to use the .json() method on the express object. But why do we pass it into app.use()? This means that every request, no matter the verb, will use the express.json() function. This is known as middleware.
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.json()) //used on every request
const dateLogger = (req, res, next) => {
const date = new Date().toLocaleDateString()
const time = new Date().toLocaleTimeString()
console.log(`${req.method} ${req.path} ran on ${date} at ${time}`)
next()
}
app.get('/', dateLogger, (req, res) => {
// ACTION TO BE PERFORMED
})
// dateLogger is ran anytime we receive a GET request on the '/' route
What was that next() function?
const dateLogger = (req, res, next) => {
const date = new Date().toLocaleDateString()
const time = new Date().toLocaleTimeString()
console.log(`${req.method} ${req.path} ran on ${date} at ${time}`)
next()
}
app.get('/', dateLogger, (req, res) => {
// ACTION TO BE PERFORMED
})
Looking back at our example, we see that are passing in a new param, called next, to our callback function.
Next basically says "Move onto the next applicable route or function"
const dateLogger = (req, res, next) => {
const date = new Date().toLocaleDateString()
const time = new Date().toLocaleTimeString()
console.log(`${req.method} ${req.path} ran on ${date} at ${time}`)
next()
}
app.get('/', dateLogger, (req, res) => {
// ACTION TO BE PERFORMED
})
Server: Okay, looks like we received a GET request to the '/' route. Let's run the dateLogger function
dateLogger: I am going to log the date and time and then tell the next function in line to run once I have completed what I need to. Next()
Server: Run callback, run (req, res) => {}
What if we leave next() out?
The next function in line, whether on the same or different route, run.
const express = require('express')
require('dotenv').config()
const app = express()
app.use(express.json())
const checkAdmin = (req, res, next) => {
const {isAdmin} = req.body
if(isAdmin){
// DO ADMIN STUFF, if necessary
next()
}
// TELL THAT FOOL TO GET LOST
}
app.get('/admin', checkAdmin, (req, res) => {
res.send('Take a look at this admin stuff')
})
const PORT = process.env.SERVER_PORT
app.listen(PORT, () => console.log(`magic is happening on ${PORT}))
*As Frank
const express = require('express')
require('dotenv').config()
const session = require('session')
const app = express()
app.use(express.json())
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 1000 * 60 * 60
}
}))
const viewCount = (req, res, next) => {
if(req.session.views){
req.session.views++
} else {
req.session.views = 1
}
next()
}
app.get('/', viewCount, (req, res) => {
res.send(`You have viewed this page ${req.session.views} times`)
})
const PORT = process.env.SERVER_PORT
app.listen(PORT, () => console.log(`it vas lit on port ${PORT}`))
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 1000 * 60 * 60
}
}))