Repository navigation
London | 26-SDC-July | Raihan Sharif | Sprint 3 | Middleware exercises #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RaihanSharif
wants to merge
55
commits into
CodeYourFuture:main
Choose a base branch
from
RaihanSharif:middleware
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
1d38a28
backend boilerplate
RaihanSharif 919a6d9
frontend boilerplate
RaihanSharif d5af26b
define message shape
RaihanSharif 5200c6f
post new message
RaihanSharif ef6d1e5
Finish non-interactive backend
RaihanSharif 507640d
Merge branch 'main' into frontend
RaihanSharif d7df7b5
complete frontend layout and from
RaihanSharif e42720b
non-live chat app done
RaihanSharif 49ceee5
dockerfile for deployment
RaihanSharif 8f48f94
environment variables
RaihanSharif fe110b9
install dotenv
RaihanSharif 40dbadd
fetch from production backend
RaihanSharif b5aec51
short polling
RaihanSharif d31c582
optional long polling
RaihanSharif afd1068
no live update likes
RaihanSharif 80805e0
sort of working live update reactions
RaihanSharif be3e08f
Define message class with autoincrement id
RaihanSharif 4306653
rebuild using integer id and Message class
RaihanSharif 590918b
store the id of the last fetched message in state.
RaihanSharif 0f9a390
add validation for getMessages
RaihanSharif 630a3ae
no sleep before next fetch
RaihanSharif 0f632b0
Errors and validatoin
RaihanSharif 034791b
Use REST style API endpoint
RaihanSharif 8b2c69d
frontend error handling
RaihanSharif 09d08ab
production backend url
RaihanSharif 4f625eb
Clean up model
RaihanSharif 065aa34
Use event streaming and REST for messages
RaihanSharif 06b6430
use Map instead of list to store messages.
RaihanSharif ef22de1
addMessage takes in username and msg_body as separate parameters
RaihanSharif ef2383b
when message sent, immediately add to DOM
RaihanSharif b46622d
regular polling of the event stream
RaihanSharif 67ff4c3
event driven likes/dislikes
RaihanSharif 90417f9
update model and app to better handle message ID errors
RaihanSharif 69f05c2
code clean up and comments
RaihanSharif 2e7dc03
Merge pull request #1 from RaihanSharif/event-driven
RaihanSharif 8fa296a
production backend URL
RaihanSharif 1756f54
return after catching failed message send
RaihanSharif ee4caca
basic version working
RaihanSharif 8717b46
send messages from client cursor so that there are no missing messages.
RaihanSharif b551c70
update subscribe logic and add restarting websocket from client side
RaihanSharif e801458
remove unnecessary check
RaihanSharif 425817f
remove comment
RaihanSharif 5f28f18
Refactor: websocket version of backend.
RaihanSharif 2180255
extract common code out of web socket frontend script
RaihanSharif 806633e
extract out express application
RaihanSharif 3361d8e
polling backend done
RaihanSharif f98b3e0
separate polling & websocket frontend and backend
RaihanSharif 6ffe037
Merge pull request #2 from RaihanSharif/refactor
RaihanSharif 2dc2778
production URL
RaihanSharif a548c4d
update dockerfile to run the polling app
RaihanSharif ff350f9
custom middleware done
RaihanSharif de4e037
built-in middleware done
RaihanSharif 7f47ac6
Stop tracking vscode setting
RaihanSharif b53d94c
remove console logs, unused imports and fix response formatting.
RaihanSharif c9e4156
Remove chat-app code and use one node_modiles folder for both middlew…
RaihanSharif File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| # Logs | ||
| logs | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| lerna-debug.log* | ||
|
|
||
| # Diagnostic reports (https://nodejs.org/api/report.html) | ||
| report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | ||
|
|
||
| # Runtime data | ||
| pids | ||
| *.pid | ||
| *.seed | ||
| *.pid.lock | ||
|
|
||
| # Directory for instrumented libs generated by jscoverage/JSCover | ||
| lib-cov | ||
|
|
||
| # Coverage directory used by tools like istanbul | ||
| coverage | ||
| *.lcov | ||
|
|
||
| # nyc test coverage | ||
| .nyc_output | ||
|
|
||
| # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
| .grunt | ||
|
|
||
| # Bower dependency directory (https://bower.io/) | ||
| bower_components | ||
|
|
||
| # node-waf configuration | ||
| .lock-wscript | ||
|
|
||
| # Compiled binary addons (https://nodejs.org/api/addons.html) | ||
| build/Release | ||
|
|
||
| # Dependency directories | ||
| node_modules/ | ||
| jspm_packages/ | ||
|
|
||
| # Snowpack dependency directory (https://snowpack.dev/) | ||
| web_modules/ | ||
|
|
||
| # TypeScript cache | ||
| *.tsbuildinfo | ||
|
|
||
| # Optional npm cache directory | ||
| .npm | ||
|
|
||
| # Optional eslint cache | ||
| .eslintcache | ||
|
|
||
| # Optional stylelint cache | ||
| .stylelintcache | ||
|
|
||
| # Optional REPL history | ||
| .node_repl_history | ||
|
|
||
| # Output of 'npm pack' | ||
| *.tgz | ||
|
|
||
| # Yarn Integrity file | ||
| .yarn-integrity | ||
|
|
||
| # dotenv environment variable files | ||
| .env | ||
| .env.* | ||
| !.env.example | ||
|
|
||
| # parcel-bundler cache (https://parceljs.org/) | ||
| .cache | ||
| .parcel-cache | ||
|
|
||
| # Next.js build output | ||
| .next | ||
| out | ||
|
|
||
| # Nuxt.js build / generate output | ||
| .nuxt | ||
| dist | ||
| .output | ||
|
|
||
| # Gatsby files | ||
| .cache/ | ||
| # Comment in the public line in if your project uses Gatsby and not Next.js | ||
| # https://nextjs.org/blog/next-9-1#public-directory-support | ||
| # public | ||
|
|
||
| # vuepress build output | ||
| .vuepress/dist | ||
|
|
||
| # vuepress v2.x temp directory | ||
| .temp | ||
|
|
||
| # Sveltekit cache directory | ||
| .svelte-kit/ | ||
|
|
||
| # vitepress build output | ||
| **/.vitepress/dist | ||
|
|
||
| # vitepress cache directory | ||
| **/.vitepress/cache | ||
|
|
||
| # Docusaurus cache and generated files | ||
| .docusaurus | ||
|
|
||
| # Serverless directories | ||
| .serverless/ | ||
|
|
||
| # FuseBox cache | ||
| .fusebox/ | ||
|
|
||
| # DynamoDB Local files | ||
| .dynamodb/ | ||
|
|
||
| # Firebase cache directory | ||
| .firebase/ | ||
|
|
||
| # TernJS port file | ||
| .tern-port | ||
|
|
||
| # Stores Visual Studio Code versions used for testing Visual Studio Code extensions | ||
| .vscode-test | ||
|
|
||
| # pnpm | ||
| .pnpm-store | ||
|
|
||
| # yarn v3 | ||
| .pnp.* | ||
| .yarn/* | ||
| !.yarn/patches | ||
| !.yarn/plugins | ||
| !.yarn/releases | ||
| !.yarn/sdks | ||
| !.yarn/versions | ||
|
|
||
| # Vite files | ||
| vite.config.js.timestamp-* | ||
| vite.config.ts.timestamp-* | ||
| .vite/ | ||
|
|
||
| CORS_ORIGINS | ||
|
|
||
| .vscode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import express from "express"; | ||
|
|
||
| const app = express(); | ||
| app.use(express.json()); | ||
| app.use(express.urlencoded({ extended: true })); | ||
|
|
||
| function usernameMiddleware(req, res, next) { | ||
| const username = req.get("X-username"); | ||
| req.username = username; | ||
| next(); | ||
| } | ||
|
|
||
| app.use(usernameMiddleware); | ||
|
|
||
| app.post("/", (req, res) => { | ||
| let responseStr = ""; | ||
| if (req.username) { | ||
| responseStr = `You are authenticated as ${req.username}.\n`; | ||
| } else { | ||
| responseStr = `You are not authenticated.\n`; | ||
| } | ||
|
|
||
| if (req.body) { | ||
| responseStr = responseStr.concat( | ||
| `You have requested information about ${req.body.length} subjects: ${req.body.join(", ")}\n`, | ||
| ); | ||
| } | ||
| res.send(responseStr); | ||
| }); | ||
|
|
||
| /* | ||
| previous CURL does not work because curl --data sends 'content-type: application/x-www-form-urlencoded', is not parsed by expres.json() | ||
| but by urlencoded(), which tries to create a key:value pair set rather than an array. | ||
|
|
||
| By setting the content type explicitly, this can force the app to use the desired middleware: | ||
|
|
||
| curl -X POST http://localhost:3000/ \ | ||
| -H "Content-Type: application/json" \ | ||
| -H "X-Username: Ahmed" \ | ||
| --data '["Bees", "birds", "lizards"]' | ||
| */ | ||
|
|
||
| app.listen(3000, () => { | ||
| console.log(`Listening on port: 3000`); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import express from "express"; | ||
|
|
||
| const app = express(); | ||
|
|
||
| function usernameMiddleware(req, res, next) { | ||
| const username = req.get("X-username"); | ||
| req.username = username; | ||
| next(); | ||
| } | ||
|
|
||
| function parseBodyAsJSONArray(req, res, next) { | ||
| const chunks = []; | ||
|
|
||
| req.on("data", (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| req.on("end", () => { | ||
| const raw = Buffer.concat(chunks).toString("utf8"); | ||
|
|
||
| try { | ||
| const parsed = JSON.parse(raw); | ||
|
|
||
| if (!Array.isArray(parsed)) { | ||
| return res.status(400).send("Body must be a JSON array"); | ||
| } | ||
|
|
||
| if (parsed.some((elem) => typeof elem !== "string")) { | ||
| return res | ||
| .status(400) | ||
| .send("All elements in array must be strings"); | ||
| } | ||
|
|
||
| req.body = parsed; | ||
| next(); | ||
| } catch (e) { | ||
| return res.status(400).send("Invalid JSON string"); | ||
| } | ||
| }); | ||
|
|
||
| req.on("error", (err) => { | ||
| next(err); | ||
| }); | ||
| } | ||
|
|
||
| app.use([usernameMiddleware, parseBodyAsJSONArray]); | ||
| app.post("/", (req, res) => { | ||
| let responseStr = ""; | ||
| if (req.username) { | ||
| responseStr = `You are authenticated as ${req.username}.\n`; | ||
| } else { | ||
| responseStr = `You are not authenticated.\n`; | ||
| } | ||
|
|
||
| if (req.body) { | ||
| responseStr = responseStr.concat( | ||
| `You have requested information about ${req.body.length} subjects: ${req.body.join(", ")}\n`, | ||
| ); | ||
| } | ||
|
|
||
| res.send(responseStr); | ||
| }); | ||
|
|
||
| app.listen(3000, () => { | ||
| console.log(`Listening on port: 3000`); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.