{"componentChunkName":"component---src-components-blogpost-blogpost-jsx","path":"/blog/NodeJS4/","result":{"data":{"site":{"siteMetadata":{"title":"K011"}},"mdx":{"id":"0e670b4b-04aa-589d-b448-2b5e6cdb6eb7","body":"var _excluded = [\"components\"];\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"The Request/Response Cycle and Middleware\",\n  \"date\": \"2021-05-26\",\n  \"description\": \"What is the Request/Response Cycle\"\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, _excluded);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Request/response Cycle\"), mdx(\"h2\", null, \"What is the request/response cycle?\"), mdx(\"p\", null, \"When a request is received, the express app will create 2 objects: request object and response object, the data will be used and processed to generate a meaningful response. Then to process that data we use something called middleware which can manipulate the request object and the response object and even execute any code we put in.\"), mdx(\"h2\", null, \"Middleware\"), mdx(\"p\", null, \"middleware is a function which can be executed before or after the handling of a route, so middleware is not only used for request and response objects, but it is usually used for this.\\nIt is used to verify access levels before entering a route, validate data, handle errors.\\nMiddeware accesses the Request, Response, next objects.\"), mdx(\"p\", null, \"Some examples of middleware in Express are,\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"body parse: This gives access to the body information of a POST request.\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"logging: for a logging system.\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"setting headers\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"router: for example the widely used res.send().\\nAll middleware we use is called \", mdx(\"strong\", {\n    parentName: \"li\"\n  }, \"middleware stack\"), \".\")), mdx(\"p\", null, \"The request and response objects process all the middleware stack step by step until they go through it all, after that they send the response and finish the cycle (we call this request-Response cycle).\"), mdx(\"p\", null, \"An example of middleware is the \"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"app.use(express.json())\\n\")), mdx(\"p\", null, \"We can create our own middleware functions\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"app.use((req,res,next)=>) \\n\\n\")));\n}\n;\nMDXContent.isMDXComponent = true;","frontmatter":{"title":"The Request/Response Cycle and Middleware","date":"2021-05-26","description":"What is the Request/Response Cycle"}},"allMdx":{"edges":[{"node":{"frontmatter":{"title":"Classification","date":"2021-11-03","description":"Naive Bayes, Discriminant Analysis, Logistic Regression"},"fields":{"slug":"/blog/est4/"}}},{"node":{"frontmatter":{"title":"Regression and Prediction","date":"2021-11-02","description":"REGRESSION"},"fields":{"slug":"/blog/est3/"}}},{"node":{"frontmatter":{"title":"Distribution","date":"2021-11-01","description":"Data distributions"},"fields":{"slug":"/blog/est1/"}}},{"node":{"frontmatter":{"title":"Stats Testing","date":"2021-11-01","description":"Statistical Experiments and significance testing"},"fields":{"slug":"/blog/est2/"}}},{"node":{"frontmatter":{"title":"Recursion","date":"2021-08-26","description":"Algorithm recursion"},"fields":{"slug":"/blog/alg1/"}}}]}},"pageContext":{"slug":"/blog/NodeJS4/","id":"0e670b4b-04aa-589d-b448-2b5e6cdb6eb7"}},"staticQueryHashes":["63159454"]}