Dependencies used:
- dotenv
- method-override
- nodemon
REST just a set of principles that describe how networked resources are accessed and manipulated. We have 7 RESTful routes that allow us basic operations for reading and manipulating a collection of data:
URL | HTTP Verb | Action |
---|---|---|
/photos/ | GET | index |
/photos/:id | GET | show |
/photos/new | GET | new |
/photos | POST | create |
/photos/:id/edit | GET | edit |
/photos/:id | PATCH/PUT | update |
/photos/:id | DELETE | destroy |
app.use((req, res, next) => {
console.log("I run for all routes");
next();
});
// tells express to try to match requests with files in the directory called 'public'
app.use(express.static("public"));
// forms cant make DELETE request by default. Only POST & GET
// methodOverride package can work around this
const methodOverride = require("method-override");
app.use(methodOverride("_method"));
/*
Our POST request is sending in data (an object with key value pairs) in the request body.
However, the HTTP protocol just sends ( url encoded) strings.
Our express app has no idea what to do with this string.
The below code take incoming strings from the body that are url encoded and parse them into an
object that can be accessed in the request parameter as a property called body.
*/
app.use(express.urlencoded({ extended: false }));
For each new key/value pair, add a new -d property="value"
argument
curl -X POST -d name="kiwi" -d color="green" -d readyToEat="on" localhost:3000/fruits
curl -X DELETE localhost:3000/fruits/1
curl -X PUT -d name="tomato" -d color="red" localhost:3000/fruits/2