#100Devs - Express & CRUD (cohort 2)

Class 40 of our Free Web Dev Bootcamp for folx affected by the pandemic. Join live T/Th 6:30pm ET leonnoel.com/twitch and ask questions here: leonnoel.com/discord

Skip to first slide
Slide 1 of 52

 

Express & CRUD

 

Leon Noel

#100Devs

We come from poverty, man, we ain't have a thing
It's a lot of animosity, but they won't say my name


Slide 2 of 52

Agenda


Slide 3 of 52

Questions

About last class or life


Slide 4 of 52

Checking In

Like and Retweet the Tweet

!checkin


Slide 5 of 52

Submitting Work

API Homework

 


Slide 6 of 52

Homework

(should be done)

Philip Roberts

Zell Liew

Jake Archibald

 


Slide 7 of 52

Thank You

 

Zell Liew


Slide 8 of 52

Summer Break

Break, Catch Up, Professional Stuff, 100Hours Project, and Hitlist


Slide 9 of 52

Backend!


Slide 10 of 52

NODE.js BABY


Slide 11 of 52

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.


Slide 12 of 52

Engine Vs. Compiler


Slide 13 of 52

And just like the browser's Web APIs Node come with a bunch of stuff


Slide 14 of 52

Built in Modules

(libraries or collections of functions)

 

HTTP (network access)

FS (file system access)


Slide 15 of 52

Access to millions of packages via NPM

(groupings of one or more custom modules)


Slide 16 of 52

Not Web APIs, but C/C++ APIs


Slide 17 of 52

sorry, don't remember the source


Slide 18 of 52

Let's Code

Simple Node Server


Slide 19 of 52

Just

HTTP & FS

const http = require('http')
const fs = require('fs')
http.createServer((req, res) => {
  fs.readFile('demofile.html', (err, data) => {
    res.writeHead(200, {'Content-Type': 'text/html'})
    res.write(data)
    res.end()
  })
}).listen(8000)

Slide 20 of 52

How could we clean this up?


Slide 21 of 52

Express


Slide 22 of 52

Express

Fast, unopinionated, minimalist web framework for Node.js

 

With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy.


Slide 23 of 52

But First

Blast To The Past


Slide 24 of 52

Slide 25 of 52

How Does The Internet Work


Slide 26 of 52

"It is a set of rules that allow programs to talk to each other. The developer creates the API on the server and allows the client to talk to it."


Slide 27 of 52

CRUD

Create (post) - Make something

Read (get) - Get Something

Update (put) - Change something

Delete (delete) - Remove something


Slide 28 of 52

Mongo DB

Collection

document

document

document

document


Slide 29 of 52

EJS

(Embedded Javascript Templates)

    <h1>Current Rappers</h1>
    <ul class="rappers">
    <% for(let i=0; i < info.length; i++) {%>
        <li class="rapper">
            <span><%= info[i].stageName %></span>
            <span><%= info[i].birthName %></span>
            <span class='del'>delete</span>
        </li>
    <% } %>
    </ul>

    <h2>Add A Rapper:</h2>

Slide 30 of 52

Client (Laptop)

Browser

URL

Server

Disk

API Code

Files

Mongo

Database

Collection

Collection

document

document

document

document

app.get('/')

app.post('/form')

app.put('/info')

app.delete('/info')

/views

/public

index.ejs

main.js

style.css

Fonts

Images


Slide 31 of 52

Rap Names

What are some Read (get) requests?


Slide 32 of 52

Slide 33 of 52

Rap Names

What are some Create (post) requests?


Slide 34 of 52

Slide 35 of 52

Rap Names

What are some Delete (delete) requests?


Slide 36 of 52

Slide 37 of 52

Rap Names

What are some Update (put) requests?


Slide 38 of 52

Slide 39 of 52

Client (Laptop)

Browser

URL

Server

Disk

API Code

Files

Mongo

Database

Collection

Collection

document

document

document

document

app.get('/')

app.post('/form')

app.put('/info')

app.delete('/info')

/views

/public

index.ejs

main.js

style.css

Fonts

Images


Slide 40 of 52

Let's Build An App with Express


Slide 41 of 52

Key Steps


Slide 42 of 52
mkdir api-project
cd api-project
npm init 
npm install express --save
npm install mongodb --save
npm install ejs --save

Setting Up The Project


Slide 43 of 52
let db,
    dbConnectionStr = 'mongodb+srv://demo:demo@cluster0
    .2wapm.mongodb.net/rap?retryWrites=true&w=majority',
    dbName = 'rap'

MongoClient.connect(dbConnectionStr, { useUnifiedTopology: true })
    .then(client => {
        console.log(`Connected to ${dbName} Database`)
        db = client.db(dbName)
    })

Connect To DB


Slide 44 of 52
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())



app.listen(process.env.PORT || PORT, ()=>{
    console.log(`Server running on port ${PORT}`)
})

Setup Server


Slide 45 of 52
app.get('/',(request, response)=>{
    db.collection('rappers').find().toArray()
    .then(data => {
        response.render('index.ejs', { info: data })
    })
    .catch(error => console.error(error))
})

API - GET


Slide 46 of 52
app.post('/addRapper', (request, response) => {
    db.collection('rappers').insertOne(request.body)
    .then(result => {
        console.log('Rapper Added')
        response.redirect('/')
    })
    .catch(error => console.error(error))
})

API - POST


Slide 47 of 52

Create EJS File

    <h1>Current Rappers</h1>
    <ul class="rappers">
    <% for(let i=0; i < info.length; i++) {%>
        <li class="rapper">
            <span><%= info[i].stageName %></span>
            <span><%= info[i].birthName %></span>
            <span class='del'>delete</span>
        </li>
    <% } %>
    </ul>

    <h2>Add A Rapper:</h2>

Slide 48 of 52

Create Public Folder/Files


Slide 49 of 52
app.delete('/deleteRapper', (request, response) => {
    db.collection('rappers').deleteOne({stageName: request.body.stageNameS})
    .then(result => {
        console.log('Rapper Deleted')
        response.json('Rapper Deleted')
    })
    .catch(error => console.error(error))

})

API - DELETE


Slide 50 of 52
heroku login -i
heroku create simple-rap-api
echo "web: node server.js" > Procfile
git add . 
git commit -m "changes"
git push heroku main

Push To Heroku


Slide 51 of 52

Let's Code

Rap Names App


Slide 52 of 52

Homework

 

Do: Start prepping THE BANK

Do: Complete Your Professional Links

Create: Heroku, Mongo Atlas, and Postman Accounts

Read: https://zellwk.com/blog/crud-express-mongodb

Do: Make Your Own APP and Push To Heroku