Merge branch 'master' of github.com:thomasabishop/computer_science

This commit is contained in:
thomasabishop 2022-08-04 12:11:48 +01:00
commit cfe3e3efb5
8 changed files with 125 additions and 3 deletions

View file

@ -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. 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() {} foo() {}
bar() {} bar() {}
} }
``` ```
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.

View file

@ -0,0 +1 @@
// TODO Add details of Helmet and examples of use.

View file

@ -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

View file

@ -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",
},
];
````

View file

@ -25,7 +25,6 @@ The following Express methods correspond to the main [HTTP request types](../../
````js ````js
const express = require('express') const express = require('express')
const app = express() const app = express()
```` ````
## Our data ## Our data