From 45d8aae03ecaa95a5fe26591a6547aed9fbe672e Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 2 Jun 2017 11:33:12 -0700 Subject: [PATCH 1/6] Create goal Build Your Own Express This goal is useful to better understand express routing and middleware. Example project for goal: https://github.com/jason00111/express-clone. Skill areas (from the matrix) which this goal addresses: - NodeJS - Server Applications - APIs - HTTP --- _goals/430-Build_Your_Own_Express.md | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 _goals/430-Build_Your_Own_Express.md diff --git a/_goals/430-Build_Your_Own_Express.md b/_goals/430-Build_Your_Own_Express.md new file mode 100644 index 00000000..2b895adf --- /dev/null +++ b/_goals/430-Build_Your_Own_Express.md @@ -0,0 +1,65 @@ +--- +authors: +- "jason00111" +team_size: 1 +goal_id: 430 +title: Build Your Own Express +created_at: '2017-06-02T16:49:40.602Z' +labels: +- practice +- draft +level: '2' +published: false +redirect_from: '/goals/430' +--- + +# Build Your Own Express + +## Challenge Rating + +This goal will likely be within your ZPD if you... + +- Have used node.js +- Have used express.js +- Are interested in better understanding express middleware and routing + +## Description + +Express is a framework for building web apps and web API's using node.js. + +In this goal, you will build a simplified clone of express. + +## Context + +After doing this goal you will have a better understanding of how express handles middleware and routing. + +## Specifications + +- [ ] An express app can be initialized using +``` +const express = require('./myExpress') +const app = express() +``` +- [ ] `app` has `get`, `post`, `put`, and `delete` functions which define routing (how an app responds to api endpoints) using this format `app.METHOD(PATH, HANDLER)`. See [this](https://expressjs.com/en/starter/basic-routing.html). +- [ ] Middleware can be added to the app using `app.use` in this format: +``` +app.use(function (req, res, next) { + // do middleware things here + next() +}) +``` +- [ ] Middleware is run in the order that is defined in the code. +- [ ] The npm packages [body-parser](https://www.npmjs.com/package/body-parser) and [cookie-parser](https://www.npmjs.com/package/cookie-parser) can be added as middleware to an app using `app.use`. +- [ ] Query strings are parsed and available at `req.query` (see [this](https://expressjs.com/en/4x/api.html#req.query)) +- [ ] The path is available at `req.path` (see [this](https://expressjs.com/en/4x/api.html#req.path)) +- [ ] Route parameters can be defined with a colon by the developer in the `PATH` of `app.METHOD` functions like this `/users/:userId/books/:bookId`. Parameters are available at `req.params`. See [this](https://expressjs.com/en/guide/routing.html#route-parameters) +- [ ] The artifact produced is properly licensed, preferably with the [MIT license][mit-license]. + +## Resources + +- [Express documentation](https://expressjs.com) +- [node.js built-in url parser](https://nodejs.org/api/url.html) +- [node.js built-in query string parser](https://nodejs.org/api/querystring.html) +- [node.js built-in http server](https://nodejs.org/api/http.html) + +[mit-license]: https://opensource.org/licenses/MIT From 96401d2833742705fcc403b7d3b9b904f9e4bf29 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 2 Jun 2017 12:25:51 -0700 Subject: [PATCH 2/6] Adds more specs for responses --- _goals/430-Build_Your_Own_Express.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/_goals/430-Build_Your_Own_Express.md b/_goals/430-Build_Your_Own_Express.md index 2b895adf..b5134125 100644 --- a/_goals/430-Build_Your_Own_Express.md +++ b/_goals/430-Build_Your_Own_Express.md @@ -53,6 +53,20 @@ app.use(function (req, res, next) { - [ ] Query strings are parsed and available at `req.query` (see [this](https://expressjs.com/en/4x/api.html#req.query)) - [ ] The path is available at `req.path` (see [this](https://expressjs.com/en/4x/api.html#req.path)) - [ ] Route parameters can be defined with a colon by the developer in the `PATH` of `app.METHOD` functions like this `/users/:userId/books/:bookId`. Parameters are available at `req.params`. See [this](https://expressjs.com/en/guide/routing.html#route-parameters) +- [ ] A plain text response can be sent with `res.send` +- [ ] Response status can be set like this (see [this](https://expressjs.com/en/4x/api.html#res.status)) +``` +res.status(200) +res.send('Hello, World!') +``` +or this +``` +res.status(200).send('Hello, World!') +``` +- [ ] A json response can be sent with `res.json` (see [this](https://expressjs.com/en/4x/api.html#res.json)) +- [ ] HTTP `Content-Type` header can be set with `res.type` (see [this](https://expressjs.com/en/4x/api.html#res.type)) +- [ ] HTTP headers can be set with `res.set` (see [this](https://expressjs.com/en/4x/api.html#res.set)) +- [ ] An HTML file can be sent with `res.sendFile(pathToFile)` - [ ] The artifact produced is properly licensed, preferably with the [MIT license][mit-license]. ## Resources From e501388ab19e81b68103f9019112cc6c5d3761cb Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 2 Jun 2017 13:30:30 -0700 Subject: [PATCH 3/6] Update spec for res.send --- _goals/430-Build_Your_Own_Express.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_goals/430-Build_Your_Own_Express.md b/_goals/430-Build_Your_Own_Express.md index b5134125..591dd0a1 100644 --- a/_goals/430-Build_Your_Own_Express.md +++ b/_goals/430-Build_Your_Own_Express.md @@ -53,7 +53,7 @@ app.use(function (req, res, next) { - [ ] Query strings are parsed and available at `req.query` (see [this](https://expressjs.com/en/4x/api.html#req.query)) - [ ] The path is available at `req.path` (see [this](https://expressjs.com/en/4x/api.html#req.path)) - [ ] Route parameters can be defined with a colon by the developer in the `PATH` of `app.METHOD` functions like this `/users/:userId/books/:bookId`. Parameters are available at `req.params`. See [this](https://expressjs.com/en/guide/routing.html#route-parameters) -- [ ] A plain text response can be sent with `res.send` +- [ ] `res.send` can send plain text, html, or json - [ ] Response status can be set like this (see [this](https://expressjs.com/en/4x/api.html#res.status)) ``` res.status(200) From 8cc5b2bb80c2e47b9ada062af0cc173485e40cd6 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 2 Jun 2017 13:33:01 -0700 Subject: [PATCH 4/6] Add link to req.send spec --- _goals/430-Build_Your_Own_Express.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_goals/430-Build_Your_Own_Express.md b/_goals/430-Build_Your_Own_Express.md index 591dd0a1..3ac16370 100644 --- a/_goals/430-Build_Your_Own_Express.md +++ b/_goals/430-Build_Your_Own_Express.md @@ -53,7 +53,7 @@ app.use(function (req, res, next) { - [ ] Query strings are parsed and available at `req.query` (see [this](https://expressjs.com/en/4x/api.html#req.query)) - [ ] The path is available at `req.path` (see [this](https://expressjs.com/en/4x/api.html#req.path)) - [ ] Route parameters can be defined with a colon by the developer in the `PATH` of `app.METHOD` functions like this `/users/:userId/books/:bookId`. Parameters are available at `req.params`. See [this](https://expressjs.com/en/guide/routing.html#route-parameters) -- [ ] `res.send` can send plain text, html, or json +- [ ] `res.send` can send plain text, html, or json (see [this](https://expressjs.com/en/4x/api.html#res.send)) - [ ] Response status can be set like this (see [this](https://expressjs.com/en/4x/api.html#res.status)) ``` res.status(200) From a6baa36350b1feaccfeee049f8ceee1d89d539d3 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 2 Jun 2017 14:16:00 -0700 Subject: [PATCH 5/6] Adds spec for express.static --- _goals/430-Build_Your_Own_Express.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_goals/430-Build_Your_Own_Express.md b/_goals/430-Build_Your_Own_Express.md index 3ac16370..092893a2 100644 --- a/_goals/430-Build_Your_Own_Express.md +++ b/_goals/430-Build_Your_Own_Express.md @@ -67,6 +67,7 @@ res.status(200).send('Hello, World!') - [ ] HTTP `Content-Type` header can be set with `res.type` (see [this](https://expressjs.com/en/4x/api.html#res.type)) - [ ] HTTP headers can be set with `res.set` (see [this](https://expressjs.com/en/4x/api.html#res.set)) - [ ] An HTML file can be sent with `res.sendFile(pathToFile)` +- [ ] Middleware for serving static files is available at `express.static(dirPath)` (see [this](https://expressjs.com/en/4x/api.html#express.static)) - [ ] The artifact produced is properly licensed, preferably with the [MIT license][mit-license]. ## Resources From d8525af67b0124fbb2021c7e94cb456cb2c4093a Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 2 Jun 2017 15:34:42 -0700 Subject: [PATCH 6/6] adds spec for regex support for app.method --- _goals/430-Build_Your_Own_Express.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_goals/430-Build_Your_Own_Express.md b/_goals/430-Build_Your_Own_Express.md index 092893a2..27c42728 100644 --- a/_goals/430-Build_Your_Own_Express.md +++ b/_goals/430-Build_Your_Own_Express.md @@ -68,6 +68,7 @@ res.status(200).send('Hello, World!') - [ ] HTTP headers can be set with `res.set` (see [this](https://expressjs.com/en/4x/api.html#res.set)) - [ ] An HTML file can be sent with `res.sendFile(pathToFile)` - [ ] Middleware for serving static files is available at `express.static(dirPath)` (see [this](https://expressjs.com/en/4x/api.html#express.static)) +- [ ] `app`'s `get`, `post`, `put`, and `delete` functions can take a regular expression as first argument to match endpoints - [ ] The artifact produced is properly licensed, preferably with the [MIT license][mit-license]. ## Resources