diff --git a/Programming_Languages/NodeJS/Modules/Modules.md b/Programming_Languages/NodeJS/Modules/Modules.md index 8eca718..c345c41 100644 --- a/Programming_Languages/NodeJS/Modules/Modules.md +++ b/Programming_Languages/NodeJS/Modules/Modules.md @@ -146,7 +146,7 @@ module.exports = { } ``` -**Both of these structures would be referred to in the same way when importing and using them** +**Both of these structures would be referred to in the same way when importing and using them.** Or you could export an actual class as the default. This is practically the same as the two above other than that you would have to use `new` to initiate an instance of the class. @@ -155,4 +155,5 @@ export default class { foo() {} bar() {} } -``` \ No newline at end of file +``` +Every method and property within the export will be public by default, whether it is an object, class or function. If you wanted to keep certain methods/properties private, the best approach is to define them as variables and functions within the module file but outside of the `export` block. \ No newline at end of file diff --git a/Programming_Languages/NodeJS/Package_management.md b/Programming_Languages/NodeJS/Modules/Package_management.md similarity index 100% rename from Programming_Languages/NodeJS/Package_management.md rename to Programming_Languages/NodeJS/Modules/Package_management.md diff --git a/Programming_Languages/NodeJS/Modules/Third_party/Helmet.md b/Programming_Languages/NodeJS/Modules/Third_party/Helmet.md new file mode 100644 index 0000000..58bec37 --- /dev/null +++ b/Programming_Languages/NodeJS/Modules/Third_party/Helmet.md @@ -0,0 +1 @@ +// TODO Add details of Helmet and examples of use. \ No newline at end of file diff --git a/Programming_Languages/NodeJS/REST_APIs/01_GET.md b/Programming_Languages/NodeJS/REST_APIs/01_GET.md new file mode 100644 index 0000000..43b5ae3 --- /dev/null +++ b/Programming_Languages/NodeJS/REST_APIs/01_GET.md @@ -0,0 +1,69 @@ +--- +tags: + - Programming_Languages + - backend + - node-js + - express + - REST + - apis +--- + +# Creating a REST API with Node and Express: GET requests + +With our GET request we will simply return the array of course objects. + +We create an [event emitter](Events%20module.md#event-emitters) and listener that listens for GET requests on a specified port and sends data in response to requests. + +```js +// Return a value as response from specified URI +app.get('/api/courses', (req, res) => { + res.send(courses) +}) + +app.listen(3000, () => console.log('Listening on port 30000...')) +``` + +Our server is now set up: + +![](/img/server-listening.png) + + +> When creating our API this structure of creating handlers for specific routes will be iterated. Every endpoint will be specified with `[app].[http_request_type]` and followed by a callback. + +We can now call the endpoint: + +```js +const getAllCourses = async () => { + try { + const resp = await axios.get("http://localhost:3000/api/courses"); + console.log(resp.data); + } catch (err) { + console.error(err); + } +}; + +getAllCourses(); +``` +Returns: + +```js +[ + { id: 1, name: 'First course' }, + { id: 2, name: 'Second course' }, + { id: 3, name: 'Third course' } +] +``` + +## Parameters + +The previous example serves the entire set of our data. But we will also need to retrieve specific values, we do this by adapting the GET callback to accept parameters. These parameters will correspond to the specific entry in our main data array. + +````js +app.get("/api/courses/:id", (req, res) => { + res.send(req.params.id); +}); +```` + +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. + +Here is a more detailed example (with a more complex dummy dataset), this time with more than one parameter \ No newline at end of file diff --git a/Programming_Languages/NodeJS/REST_APIs/0_Introduction.md b/Programming_Languages/NodeJS/REST_APIs/0_Introduction.md new file mode 100644 index 0000000..a65ed6c --- /dev/null +++ b/Programming_Languages/NodeJS/REST_APIs/0_Introduction.md @@ -0,0 +1,52 @@ +--- +tags: + - Programming_Languages + - backend + - node-js + - express + - REST + - apis +--- + +# Creating a REST API with Node and Express: Introduction + +We are going to use Express to create a [RESTful API](/Databases/RESTful_APIs.md) in Node.js. + +## Request types + +Express provides us with methods corresponding to each of the [HTTP request types](/Databases/HTTP_request_types.md): + +* `app.get()` +* `app.post()` +* `app.put()` +* `app.delete()` + +## Creating an Express instance + +````js +const express = require('express') +const app = express() +```` + +## Our data set + +> Typically when you create a RESTful API you are going to be returning data from a database. For simplicity we are just going simulate this with a simple data array so that we can focus on the Express syntax rather than database handling. + +We will mainly work with the following array of objects: + +````js +const courses = [ + { + id: 1, + name: "First course", + }, + { + id: 2, + name: "Second course", + }, + { + id: 3, + name: "Third course", + }, +]; +```` \ No newline at end of file diff --git a/Programming_Languages/NodeJS/REST/Create_RESTful_API_with_Express.md b/Programming_Languages/NodeJS/REST_APIs/Create_RESTful_API_with_Express.md similarity index 99% rename from Programming_Languages/NodeJS/REST/Create_RESTful_API_with_Express.md rename to Programming_Languages/NodeJS/REST_APIs/Create_RESTful_API_with_Express.md index 776ea58..6ee88ff 100644 --- a/Programming_Languages/NodeJS/REST/Create_RESTful_API_with_Express.md +++ b/Programming_Languages/NodeJS/REST_APIs/Create_RESTful_API_with_Express.md @@ -25,7 +25,6 @@ The following Express methods correspond to the main [HTTP request types](../../ ````js const express = require('express') const app = express() - ```` ## Our data diff --git a/Programming_Languages/NodeJS/REST/Full_example.md b/Programming_Languages/NodeJS/REST_APIs/Full_example.md similarity index 100% rename from Programming_Languages/NodeJS/REST/Full_example.md rename to Programming_Languages/NodeJS/REST_APIs/Full_example.md diff --git a/Programming_Languages/NodeJS/REST/Validation.md b/Programming_Languages/NodeJS/REST_APIs/Validation.md similarity index 100% rename from Programming_Languages/NodeJS/REST/Validation.md rename to Programming_Languages/NodeJS/REST_APIs/Validation.md