diff --git a/Programming_Languages/NodeJS/Middlewear.md b/Programming_Languages/NodeJS/Middlewear.md index e44575c..b365a02 100644 --- a/Programming_Languages/NodeJS/Middlewear.md +++ b/Programming_Languages/NodeJS/Middlewear.md @@ -3,23 +3,23 @@ tags: - Programming_Languages - backend - node-js - - middlewear + - middleware --- -## What is middlewear? +## What is middleware? * Anything that terminates the `req, res` cycle counts as middleware. It is basically anything that acts as an intermediary once the request is received but before the resource is sent. A good example would be the `app.use(express.json()` or `app.use(bodyParser.json)` functions we call in order to be able to parse JSON that is sent from the client. -* You will most likely have multiple middlewear functions running at once. We call this intermediary part of the cycle the **request processing pipeline**. -* Generally all middlewear will be added as a property on the Express `app` instance with the `app.use(...)` syntax. +* You will most likely have multiple middleware functions running at once. We call this intermediary part of the cycle the **request processing pipeline**. +* Generally all middleware will be added as a property on the Express `app` instance with the `app.use(...)` syntax. -## Creating custom middlewear functions +## Creating custom middleware functions ### Basic schema ````js app.use((req, res, next) => { - // do some middlewear + // do some middleware next() }) @@ -27,7 +27,7 @@ app.use((req, res, next) => { ### `next` -The `next` parameter is key, it allows Express to move onto the next middlewear function once the custom middlewear executes. Without it, the request processing pipeline will get blocked. Middlewear functions are basically asynchronous requests and as such they use a similar syntax as Promises (e.g `then`) for sequencing processes. +The `next` parameter is key, it allows Express to move onto the next middleware function once the custom middleware executes. Without it, the request processing pipeline will get blocked. middleware functions are basically asynchronous requests and as such they use a similar syntax as Promises (e.g `then`) for sequencing processes. ### Example of sequence @@ -53,9 +53,9 @@ Do process B... ```` > - > It makes more sense of course to define our middlewear within a function and then pass it as an argument to `app.use()` + > It makes more sense of course to define our middleware within a function and then pass it as an argument to `app.use()` -## Useful built-in middlewear +## Useful built-in middleware ### `express.static()` @@ -71,17 +71,17 @@ We can expose this to express with `app.use(static('public'))`. Then if we navig > > `app.use(express.urlencoded())` -Generally we handle the data of API requests via a JSON body and the `express.json()` middlewear. However, in cases where the data is sent from the client in the form of `key=value&key=value` appendages to the request URL, `urlencoded` allows us to parse them. +Generally we handle the data of API requests via a JSON body and the `express.json()` middleware. However, in cases where the data is sent from the client in the form of `key=value&key=value` appendages to the request URL, `urlencoded` allows us to parse them. -## Third-party middlewear +## Third-party middleware ### Helmet -Helmet is middlewear that makes it easier to set HTTP headers. +Helmet is middleware that makes it easier to set HTTP headers. ### Morgan -Morgan is middlewear that is used to log HTTP requests to the Express instance. +Morgan is middleware that is used to log HTTP requests to the Express instance. ```js app.use(morgan('dev')) ``` @@ -94,8 +94,34 @@ This uses the `tiny` default which logs the bare minimum giving us: request type It defaults to logging on the console but can also be configured to write to a log file. -## Including middlewear based on environment +## Including middleware based on environment With a full-scale Node application you will typically run three environments: * Development * Testing -* Production \ No newline at end of file +* Production + +We will not want to run certain types of middleware in all environments. For example, it would be costly to run logging in the app's production environment. It would make more sense to run this only in development. + +### Accessing current Node environment + We can control which middleware we run via the Node envrionment variables: `process.env` (see for instance [ports](./Ports.md)). + +To determine the current environment we can use the variable `process.env.NODE_ENV`. This works globally regardless of the kind of Node app we are building. But in Express, there is a built in method for retrieving the current envrionment: `app.get('env')`. + +If you haven't manually set up your environments Node will return `undefined` but express defaults to `development`. + +```js + +console.log(process.env.NODE_ENV); // undefined +console.log(app.get("env")); // development + +``` + +### +We can set Morgan to run only in development with: + +```js +if (app.get("env")) { + app.use(morgan("common")); + console.log('Morgan enabled') +} +``` diff --git a/pdf/Algorithmic_complexity.pdf b/pdf/Algorithmic_complexity.pdf index 5648cbd..e16e237 100644 Binary files a/pdf/Algorithmic_complexity.pdf and b/pdf/Algorithmic_complexity.pdf differ diff --git a/pdf/Arrays.pdf b/pdf/Arrays.pdf index 2970104..0f8adaf 100644 Binary files a/pdf/Arrays.pdf and b/pdf/Arrays.pdf differ diff --git a/pdf/Assembly_programs.pdf b/pdf/Assembly_programs.pdf index 21cd048..330ef7b 100644 Binary files a/pdf/Assembly_programs.pdf and b/pdf/Assembly_programs.pdf differ diff --git a/pdf/Atomic_and_molecular_sentences.pdf b/pdf/Atomic_and_molecular_sentences.pdf index f1e30c7..856196e 100644 Binary files a/pdf/Atomic_and_molecular_sentences.pdf and b/pdf/Atomic_and_molecular_sentences.pdf differ diff --git a/pdf/Axioms_of_set_theory.pdf b/pdf/Axioms_of_set_theory.pdf index ee0aaa5..42376da 100644 Binary files a/pdf/Axioms_of_set_theory.pdf and b/pdf/Axioms_of_set_theory.pdf differ diff --git a/pdf/Basic_database_concepts.pdf b/pdf/Basic_database_concepts.pdf index 6311a44..2d76cd4 100644 Binary files a/pdf/Basic_database_concepts.pdf and b/pdf/Basic_database_concepts.pdf differ diff --git a/pdf/Basic_properties_of_sets.pdf b/pdf/Basic_properties_of_sets.pdf index 5aebc24..7ffc647 100644 Binary files a/pdf/Basic_properties_of_sets.pdf and b/pdf/Basic_properties_of_sets.pdf differ diff --git a/pdf/Biconditional_Elimination.pdf b/pdf/Biconditional_Elimination.pdf index abd7af3..3ee8bee 100644 Binary files a/pdf/Biconditional_Elimination.pdf and b/pdf/Biconditional_Elimination.pdf differ diff --git a/pdf/Biconditional_Introduction.pdf b/pdf/Biconditional_Introduction.pdf index fb20082..be86a9d 100644 Binary files a/pdf/Biconditional_Introduction.pdf and b/pdf/Biconditional_Introduction.pdf differ diff --git a/pdf/Binary_arithmetic.pdf b/pdf/Binary_arithmetic.pdf index 504d3a5..23e04f2 100644 Binary files a/pdf/Binary_arithmetic.pdf and b/pdf/Binary_arithmetic.pdf differ diff --git a/pdf/Binary_arithmetic_with_circuits.pdf b/pdf/Binary_arithmetic_with_circuits.pdf index 86ec6d4..4255007 100644 Binary files a/pdf/Binary_arithmetic_with_circuits.pdf and b/pdf/Binary_arithmetic_with_circuits.pdf differ diff --git a/pdf/Bits,_bytes,_nibbles.pdf b/pdf/Bits,_bytes,_nibbles.pdf index 0bda614..09dea22 100644 Binary files a/pdf/Bits,_bytes,_nibbles.pdf and b/pdf/Bits,_bytes,_nibbles.pdf differ diff --git a/pdf/Compile_from_source.pdf b/pdf/Compile_from_source.pdf index e5f9d09..d2eeab2 100644 Binary files a/pdf/Compile_from_source.pdf and b/pdf/Compile_from_source.pdf differ diff --git a/pdf/Conditional_Elimination.pdf b/pdf/Conditional_Elimination.pdf index 971e184..0d7a40f 100644 Binary files a/pdf/Conditional_Elimination.pdf and b/pdf/Conditional_Elimination.pdf differ diff --git a/pdf/Conditional_Introduction.pdf b/pdf/Conditional_Introduction.pdf index 5794b4e..583a534 100644 Binary files a/pdf/Conditional_Introduction.pdf and b/pdf/Conditional_Introduction.pdf differ diff --git a/pdf/Conjunction_Elimination.pdf b/pdf/Conjunction_Elimination.pdf index 0a86827..84ccaa0 100644 Binary files a/pdf/Conjunction_Elimination.pdf and b/pdf/Conjunction_Elimination.pdf differ diff --git a/pdf/Conjunction_Introduction.pdf b/pdf/Conjunction_Introduction.pdf index f4c98c9..0023927 100644 Binary files a/pdf/Conjunction_Introduction.pdf and b/pdf/Conjunction_Introduction.pdf differ diff --git a/pdf/Defining_a_computer.pdf b/pdf/Defining_a_computer.pdf index 4521171..3b898cf 100644 Binary files a/pdf/Defining_a_computer.pdf and b/pdf/Defining_a_computer.pdf differ diff --git a/pdf/Design_features_of_language.pdf b/pdf/Design_features_of_language.pdf index b772b4b..eeac4a4 100644 Binary files a/pdf/Design_features_of_language.pdf and b/pdf/Design_features_of_language.pdf differ diff --git a/pdf/Disjunction_Elimination.pdf b/pdf/Disjunction_Elimination.pdf index 58cb7e2..afefb99 100644 Binary files a/pdf/Disjunction_Elimination.pdf and b/pdf/Disjunction_Elimination.pdf differ diff --git a/pdf/Disjunction_Introduction.pdf b/pdf/Disjunction_Introduction.pdf index f63f6df..223c833 100644 Binary files a/pdf/Disjunction_Introduction.pdf and b/pdf/Disjunction_Introduction.pdf differ diff --git a/pdf/HTTP_request_types.pdf b/pdf/HTTP_request_types.pdf index 6b24f2a..500ea48 100644 Binary files a/pdf/HTTP_request_types.pdf and b/pdf/HTTP_request_types.pdf differ diff --git a/pdf/Indeterminacy.pdf b/pdf/Indeterminacy.pdf index c03aac2..6820251 100644 Binary files a/pdf/Indeterminacy.pdf and b/pdf/Indeterminacy.pdf differ diff --git a/pdf/Law_of_the_Excluded_Middle.pdf b/pdf/Law_of_the_Excluded_Middle.pdf index 471ff7c..0931b83 100644 Binary files a/pdf/Law_of_the_Excluded_Middle.pdf and b/pdf/Law_of_the_Excluded_Middle.pdf differ diff --git a/pdf/Logic_circuits.pdf b/pdf/Logic_circuits.pdf index 275ba46..a16e16a 100644 Binary files a/pdf/Logic_circuits.pdf and b/pdf/Logic_circuits.pdf differ diff --git a/pdf/Logic_gates.pdf b/pdf/Logic_gates.pdf index 501c409..f5d998d 100644 Binary files a/pdf/Logic_gates.pdf and b/pdf/Logic_gates.pdf differ diff --git a/pdf/Logical_possibility_and_necessity.pdf b/pdf/Logical_possibility_and_necessity.pdf index 7806d7e..745472a 100644 Binary files a/pdf/Logical_possibility_and_necessity.pdf and b/pdf/Logical_possibility_and_necessity.pdf differ diff --git a/pdf/Lovelace_quote.pdf b/pdf/Lovelace_quote.pdf index bc34dcd..77b026b 100644 Binary files a/pdf/Lovelace_quote.pdf and b/pdf/Lovelace_quote.pdf differ diff --git a/pdf/Morphology.pdf b/pdf/Morphology.pdf index f538501..fa0e038 100644 Binary files a/pdf/Morphology.pdf and b/pdf/Morphology.pdf differ diff --git a/pdf/Negation_Elimination.pdf b/pdf/Negation_Elimination.pdf index 42fcc9f..46e2f01 100644 Binary files a/pdf/Negation_Elimination.pdf and b/pdf/Negation_Elimination.pdf differ diff --git a/pdf/Negation_Introduction.pdf b/pdf/Negation_Introduction.pdf index f7157c7..46d728d 100644 Binary files a/pdf/Negation_Introduction.pdf and b/pdf/Negation_Introduction.pdf differ diff --git a/pdf/Object_language_and_meta-language.pdf b/pdf/Object_language_and_meta-language.pdf index e0c89c8..4a2c645 100644 Binary files a/pdf/Object_language_and_meta-language.pdf and b/pdf/Object_language_and_meta-language.pdf differ diff --git a/pdf/Package_management_in_Arch.pdf b/pdf/Package_management_in_Arch.pdf index fa71f2e..69b921a 100644 Binary files a/pdf/Package_management_in_Arch.pdf and b/pdf/Package_management_in_Arch.pdf differ diff --git a/pdf/Primary_key.pdf b/pdf/Primary_key.pdf index d8b627a..1c4c92a 100644 Binary files a/pdf/Primary_key.pdf and b/pdf/Primary_key.pdf differ diff --git a/pdf/Queue.pdf b/pdf/Queue.pdf index 032c8f3..f8b6e70 100644 Binary files a/pdf/Queue.pdf and b/pdf/Queue.pdf differ diff --git a/pdf/RESTful_APIs.pdf b/pdf/RESTful_APIs.pdf index 2556142..92c0adc 100644 Binary files a/pdf/RESTful_APIs.pdf and b/pdf/RESTful_APIs.pdf differ diff --git a/pdf/Recursion.pdf b/pdf/Recursion.pdf index 5d898ca..576d0ce 100644 Binary files a/pdf/Recursion.pdf and b/pdf/Recursion.pdf differ diff --git a/pdf/Reiteration.pdf b/pdf/Reiteration.pdf index 335ab9a..6e71e9c 100644 Binary files a/pdf/Reiteration.pdf and b/pdf/Reiteration.pdf differ diff --git a/pdf/SQL_syntax.pdf b/pdf/SQL_syntax.pdf index ae87034..cca60e7 100644 Binary files a/pdf/SQL_syntax.pdf and b/pdf/SQL_syntax.pdf differ diff --git a/pdf/Semantic_versioning.pdf b/pdf/Semantic_versioning.pdf index e3f266d..dc9551d 100644 Binary files a/pdf/Semantic_versioning.pdf and b/pdf/Semantic_versioning.pdf differ diff --git a/pdf/Soundness.pdf b/pdf/Soundness.pdf index 5508a51..b2125b0 100644 Binary files a/pdf/Soundness.pdf and b/pdf/Soundness.pdf differ diff --git a/pdf/Stacks.pdf b/pdf/Stacks.pdf index 32e5eac..b96a9f4 100644 Binary files a/pdf/Stacks.pdf and b/pdf/Stacks.pdf differ diff --git a/pdf/Syllogism.pdf b/pdf/Syllogism.pdf index ba080cf..6017689 100644 Binary files a/pdf/Syllogism.pdf and b/pdf/Syllogism.pdf differ diff --git a/pdf/The_Pragmatic_Programmer_1999.pdf b/pdf/The_Pragmatic_Programmer_1999.pdf index df84952..21ef6af 100644 Binary files a/pdf/The_Pragmatic_Programmer_1999.pdf and b/pdf/The_Pragmatic_Programmer_1999.pdf differ diff --git a/pdf/The_binary_number_system.pdf b/pdf/The_binary_number_system.pdf index 4040676..5eabdfd 100644 Binary files a/pdf/The_binary_number_system.pdf and b/pdf/The_binary_number_system.pdf differ diff --git a/pdf/Theorems_and_empty_sets.pdf b/pdf/Theorems_and_empty_sets.pdf index 933cf85..67233ec 100644 Binary files a/pdf/Theorems_and_empty_sets.pdf and b/pdf/Theorems_and_empty_sets.pdf differ diff --git a/pdf/Turing_machines.pdf b/pdf/Turing_machines.pdf index e71cbf8..3bb68b8 100644 Binary files a/pdf/Turing_machines.pdf and b/pdf/Turing_machines.pdf differ diff --git a/pdf/User_management.pdf b/pdf/User_management.pdf index 8540ada..acabab4 100644 Binary files a/pdf/User_management.pdf and b/pdf/User_management.pdf differ diff --git a/pdf/Von_Neumann_architecture.pdf b/pdf/Von_Neumann_architecture.pdf index 612fb80..3ba07ec 100644 Binary files a/pdf/Von_Neumann_architecture.pdf and b/pdf/Von_Neumann_architecture.pdf differ diff --git a/pdf/Why_computers_use_binary.pdf b/pdf/Why_computers_use_binary.pdf index 7897e47..0e87d03 100644 Binary files a/pdf/Why_computers_use_binary.pdf and b/pdf/Why_computers_use_binary.pdf differ