rbi-learning / Today-I-Learned

1 stars 0 forks source link

Jane Week 3 Day 2 "Today-I-Learned..." #105

Open techniJANE opened 4 years ago

techniJANE commented 4 years ago

Best practice for structuring JS code:

APIs:

API Codes: Const express = require(‘express’) //go find package called express in the npm, and store it in a constant called express Const app = express() //code explaining how api is supposed to work GET request — gets some information POST request — creating something new PUT or PATCH requests - update some DELETE request — deletes some existing information HEAD request — is everything ok? (RESTful API - representational status transfer - information that’s already stored)

GET request example: app.get(“/message”, (_request,response) => { Response.json({hello: “world”}) })

POST request example: Let counter = 0 App.post(“/counter”, (_request,response) => { counter += 1 response.json({counter: counter}) })

Building an NPI:

  1. const express = require (“express”)
  2. const app = express ()
  3. body of code - what is this API about app.get(“/tasks”) <- get the user a list of all tasks app.post(“/tasks”) <- create a new task app.patch(“/tasks/:id”) <- update something about a specific task app.delete(“/tasks/:id”) <- delete a specific task
  4. app.listen(3000, () => “Server is up and running”)

app.use(express.json()) - tells the app to be able to read incoming json requests

techniJANE commented 4 years ago

Not sure where we should be posting our homework, but this is mine: https://repl.it/@techniJANE/BackToTheFrontEnd#script.js

I tried if () {blah blah blah return} ... and it didn't work, but when I used if () {} else {} everything was working. Can we go over how to use this fancier return version of the if statement again tomorrow in class?