5. RESTful Routes

RESTful Routes

GET

  • Retrieve information
  • Data is limited and sent via query string (visible)

POST

  • Post data to the server
  • Data can be any type (json) and is sent via request body (hidden)
<body>
    <h2>GET</h2>
    <form action="http://localhost:3000/tacos" method="GET">
        <input type="text" name="meat">
        <input type="number" name="qty">
        <button>Submit</button>
    </form>
    <h2>POST</h2>
    <form action="http://localhost:3000/tacos" method="POST">
        <input type="text" name="meat">
        <input type="number" name="qty">
        <button>Submit</button>
    </form>
</body>
const express = require('express')
const app = express()

app.use(express.urlencoded({extended: true}));

app.get('/tacos', (req, res) => {
    res.send("GET /tacos response");
})

app.post('/tacos', (req, res) => {
    const {meat, qty} = req.body;
    console.log(`meat: ${meat}\nquantity: ${qty}`);
    res.send("POST /tacos reponse")
})

app.listen(3000, ()=>{
    console.log("listening on port 3000");
})

REST

  • Set of guidlines for how server and client should commuincate.
  • CRUD: create (post), read (get), update (patch), destroy (delete) NAME VERB PATH Index GET /comments list all comments New GET /comments/new form to create new comment Create POST /comments create a new comment Show GET /comments/:id get one comment Edit GET /comments/:id get one comment Update PATCH /comments/:id update one comment Destroy DELETE /comments/:id delete one comment
Written on August 17, 2021