Please enable JavaScript.
Coggle requires JavaScript to display documents.
RestFUL Routing (representational state transfer) a conventional pattern…
RestFUL Routing
(representational state transfer)
a conventional pattern for mapping routes and crud
GENERAL RULES
and miscellaneous concepts
Order of routes matters
("*")
a catch all route that will display somthing for all unknown routes typed in by a user
Route Params
a pattern that server code includes for routes
example: app.get("/r/:anything/comments/:id/:title/")
-REQ-
-the request object contains all of the incoming info about the request
TO CAPTURE REQ
req,params
app.get("/r/:anything/comments/:iid",function(req, res){
//
})
console.log(req); //huge response with all req data
console.log(req.params)// {anything: "whatever was typed in"}
instead of logging it to the console you 'capture' it to a variable and include it in the title or somthing.
var captured_data = req.params.anything
#
-RES-
similar to req, res is an object that has useful properties
-TO RENDER HTML
res.render("file name")
//Typically somthing like EJS
found in the vies directory
TO INCLUDE DATA IN HTML
res.render("file-name", {captured_data: captured_data})
TO REDIRECT
res.redirect
types of requests
POST
for sending new data to be used on the server side
<form action = "/addfriend" method="POST" >
input - NAME
*name is key sent in the body of the request
req.body.name
submit button
</form>
action should match
app.post("/addfriend", callback)
when not using database keep scope in mind
npm install body-parser
GET
retrieving information
7 Restful Routes
Show
/bogs/:id
GET
show info about one specific blog
Edit
/blogs/:id/:edit
GET
Show edit FORM for one blog
Create
/bogs
POST
create a new blog then redirect somewhere
In the form
action = where /blogs
method is how POST
Update
/blogs/:id
PUT
Update a particular blog, then redirect somewhere
New
/blogs/new
GET
Displays new blog FORM
Destroy
/dogs/:id
DELETE
Delete a particular blog, then redirect somewhere
Index
/blogs
GET
List all blogs
(shows a little of each blog)
Name
Path
HTTP Verb
Purpose
Blog Example
Index
redirect root route to /blogs
GET
find all blogs data
render ejs page W/ DATA
create template to display ALL blog samples
New
GET
RENDERs new.ejs
new.ejs contains the form to make a new blog post
form
action = /blogs
method = "POST"
inputs - name = key to access data
submit button
triggers the POST to /blogs
create
creates blog from new route post request
#
POST
Blog.create(req.body.blog, callBack)
contains:
blog[image]
blog[body]
ect...
also gives an id
redirect to index
show
GET
us Blog.findById(id, callBack)
#
renders show template
and ID
shows one full blog post
Blog shown determined by ID
edit
GET
render edit ejs
FORM
copied form from New
and pre-fill form with data via Blog.findByID()
input attr value =" blog.title"
submitting will trigger the Update PUT request
update
PUT
html forms dont support PUT request so default is GET
method over ride
npm package
include line at end of URL action in form
#
once connected to edit form
use Blog.findByIdAndUpdate(id, newData, callBack)
destroy
DELETE
method override
a delete button in show page that triggers delete route
destroy blog
Redirect back to index
via
Blog.findByIdAndRemoce(id,callBack)
Bread and butter of express apps
Nested routing
Example:
/campgrounds/:id/comments/new
route inside of a route