From 491cb388b4cfccd66f9f9dc5cd1b4fc762388c49 Mon Sep 17 00:00:00 2001 From: tactonbishop Date: Thu, 4 Aug 2022 14:00:04 +0100 Subject: [PATCH] Last Sync: 2022-08-04 14:00:04 --- .../NodeJS/REST_APIs/{01_GET.md => 1_GET.md} | 11 ++++ .../NodeJS/REST_APIs/2_PUT.md | 58 +++++++++++++++++++ 2 files changed, 69 insertions(+) rename Programming_Languages/NodeJS/REST_APIs/{01_GET.md => 1_GET.md} (87%) create mode 100644 Programming_Languages/NodeJS/REST_APIs/2_PUT.md diff --git a/Programming_Languages/NodeJS/REST_APIs/01_GET.md b/Programming_Languages/NodeJS/REST_APIs/1_GET.md similarity index 87% rename from Programming_Languages/NodeJS/REST_APIs/01_GET.md rename to Programming_Languages/NodeJS/REST_APIs/1_GET.md index a2373cf..52484ea 100644 --- a/Programming_Languages/NodeJS/REST_APIs/01_GET.md +++ b/Programming_Languages/NodeJS/REST_APIs/1_GET.md @@ -66,6 +66,17 @@ app.get("/api/courses/:id", (req, res) => { We use the `:` symbol in the URI to indicate that we looking to parse for a specific value in the data. Now if we call `/api/courses/2`, we will get the second item in the array. +The block above is the most basic format but we would want to add some kind of error handling, for example: + +```js +app.get("/api/courses/:id", (req, res) => { + const course = courses.find((c) => c.id === parseInt(req.params.id)); + if (!course) res.status(404).send("A course with the given ID was not found"); + res.send(course); +}); +``` + + ## Queries Whereas parameters return specific data points, queries don't get data they aggregate or present the data that is returned in a certain way, such as for instance applying a search function. We indicate queries with a `?` in our URI. diff --git a/Programming_Languages/NodeJS/REST_APIs/2_PUT.md b/Programming_Languages/NodeJS/REST_APIs/2_PUT.md new file mode 100644 index 0000000..7b2302f --- /dev/null +++ b/Programming_Languages/NodeJS/REST_APIs/2_PUT.md @@ -0,0 +1,58 @@ +--- +tags: + - Programming_Languages + - backend + - node-js + - express + - REST + - apis +--- + +# Creating a REST API with Node and Express: POST requests + +To demonstrate the handling of POST requests, we will create a handler that add a new element to the array of courses. + +```js +app.post('/api/courses', (req, res) => { + const course = { + id: courses.length + 1, + name: req.body.name + } + courses.push(course); + res.send(course) + +}) +``` + +Here we use the body that is sent from the client and isolate the field `name`. This presupposes that the client is sending us data with the following shape as the body: + +```json +{ + "name": "Biology and Life Sciences" +} +``` + +To execute the PUT request from the frontend: + +```js +const addCourse = async (newCourse) => { + try { + const resp = await axios.post("http://localhost:3000/api/courses", { + name: newCourse, + }); + console.log(resp.data); + } catch (err) { + console.error(err); + } +}; + +addCourse("Biology and Life Sciences"); +``` + +Which returns: + +```js +{ id: 4, name: 'Biology and Life Sciences' } +``` + +The `id` is added by the server, not the client. Having created the new value we add it to our `courses` array. (In reality we would be creating a new entry in a database.) Then we follow the convention of returning the new value back to the client. \ No newline at end of file